Skip to content

React Authentication API Reference

The @resourge/react-authentication package provides a comprehensive set of tools to manage user authentication, authorization, and error handling in React applications.


Initializes the authentication system with custom logic for profile fetching, token refresh, and storage.

import { setupAuthentication } from '@resourge/react-authentication';
const authentication = setupAuthentication({
getProfile: async (token) => {
// Retrieve user profile based on token
},
refreshToken: async (token, refreshToken) => {
// Refresh authentication token
},
storage: {
getItem: (key) => localStorage.getItem(key),
setItem: (key, value) => localStorage.setItem(key, value),
removeItem: (key) => localStorage.removeItem(key),
},
useSuspense: true, // optional, default true
});
NameTypeDescription
getProfilefunctionAsync function to fetch user profile with token
refreshTokenfunctionAsync function to refresh authentication token
storageobjectStorage interface for token persistence
useSuspensebooleanWhether to use React Suspense (default: true)

React component that wraps your app to provide authentication context, error boundaries, and suspense fallback.

PropTypeDescription
authenticationSetupAuthenticationReturnRequired. Authentication instance from setupAuthentication.
onLoginfunctionOptional. Handles user login, returns a token promise.
onLogoutfunctionOptional. Handles user logout.
getTokenfunctionOptional. Called when token is needed.
errorComponentReact element or functionOptional. Custom UI for error display.
onErrorfunctionOptional. Callback when an error occurs.
redirectOnErrorbooleanOptional. Whether to redirect after error (default: false).
import { AuthenticationSystem } from '@resourge/react-authentication';
<AuthenticationSystem
authentication={authentication}
onLogin={async (username, password) => {
// Login logic returns token string
}}
onLogout={async (token) => {
// Logout logic
}}
onError={(error) => {
console.error(error);
}}
errorComponent={<CustomErrorComponent />}
>
{/* App content */}
</AuthenticationSystem>

Provides access to authentication state and actions inside React components.

import { useAuthenticationContext } from '@resourge/react-authentication';
const { user, login, logout, token } = useAuthenticationContext();

Provides access to user permissions.

import { usePermissionsContext } from '@resourge/react-authentication';
const permissions = usePermissionsContext();

Provides authentication-related methods usable outside React components.

MethodDescription
authenticate()Fetches user profile using current token. Returns a promise.
login(user, pass)Performs login with username/email and password. Returns a boolean promise indicating success.
logout()Logs out the user. Returns a promise.
refreshToken()Refreshes auth token. Returns a boolean promise indicating success.
setToken(token, refreshToken?)Manually sets authentication tokens. Useful for custom auth like Google/Apple.
setAuthenticationError(error)Sets a custom authentication error.
getToken()Retrieves a valid token, refreshing if expired. Returns a promise resolving to token or null.
SessionService.login('user@example.com', 'password123')
.then(isLoggedIn => {
if (isLoggedIn) {
console.log('Login successful');
}
});

A React component to catch errors in the component tree and show fallback UI.

PropTypeDescription
childrenReactNodeThe child components to render
errorComponentReact element or funcCustom UI to render when an error occurs
onErrorfunctionCallback called on error with error and error info
redirectOnErrorbooleanWhether to redirect after error (default: false)
import { ErrorBoundary } from '@resourge/react-authentication';
<ErrorBoundary
errorComponent={(error) => <div>Error: {error.message}</div>}
onError={(error, info) => console.error(error, info)}
redirectOnError={true}
>
<MyComponent />
</ErrorBoundary>