SessionClient
The SessionClient is the central orchestrator for client-side session management. It manages the user's state, handles token refreshes, and coordinates with authentication adapters.
Since v9 it is generic over both the user and the credentials type: SessionClient<User, Credentials> — type it with the credentials union your adapter supports (e.g. CombinedCookieAuthCredentials for web, NativeAuthCredentials for native).
import { SessionClient } from '@archibald/auth';
const sessionClient = new SessionClient(OPTIONS);
Options
- Type:
SessionClientOptions
The SessionClientOptions object has the following properties:
| Name | Type | Default | Description |
|---|---|---|---|
| api | CreateApi | ✔️ The API creator instance for making requests. | |
| adapter | AuthAdapter class | CookieAuthAdapter | The class (not instance) of the auth adapter. |
| authenticateOnServer | boolean | false | Whether to prefetch user data on the server during SSR. |
| userCacheTime | number | 300000 (5 min) | TTL for user data in the cache (ms). |
| refresh | ValidExpirationTime | Interval for automatic token refresh (e.g., '10m'). | |
| storage | StorageAdapter class | 'local' | 'cookie' | 'cookie' | Storage adapter used for client-side token persistence. |
| oidcConfig | OidcConfig | OIDC client configuration — queryParamsWhitelist controls which query params login() may forward to GET auth/login. See OIDC Login Flow. |
Methods
initialize
Idempotently initializes the client: starts the refresh interval, performs the initial check(), and publishes the initialized event. Called automatically by useUser/SessionClientProvider.
- Returns:
Promise<void>
logIn
Authenticates the user with the provided credentials.
- Parameters:
credentials: Credentials - Returns:
Promise<AuthResponse>
With authProtocol: 'oidc' (web/cookie adapter) this triggers a full-page redirect: the call resolves { success: true, redirect: true } without setting loggedIn or publishing a login event — the session materializes on the SSR render after the IdP round trip. See OIDC Login Flow.
logOut
Terminates the user's session and cleans up local state.
- Returns:
Promise<AuthResponse>
refresh
Manually triggers a token refresh using the refresh token.
- Returns:
Promise<AuthResponse>
check
Checks if the current session is still valid by calling the backend.
- Returns:
Promise<AuthResponse>
isLoggedIn
Synchronously returns whether the user is currently considered logged in.
- Returns:
boolean
loadUser
Fetches the full user profile data from the backend.
- Returns:
Promise<User | null>
prefetchUser / refetchUser
prefetchUser() warms the user cache (used during SSR with authenticateOnServer); refetchUser(options?) invalidates and reloads the cached user.
subscribe
Subscribes to session events.
- Parameters:
events: AuthEvent[](e.g.,'login','logoff')callback: EventCallback
- Returns:
() => void(Unsubscribe function)
Usage Example
import { SessionClient, CookieAuthAdapter } from '@archibald/auth';
import { api } from './api';
const sessionClient = new SessionClient({
api,
adapter: CookieAuthAdapter,
authenticateOnServer: true,
refresh: '10m'
});
// Subscribe to login events
const unsubscribe = sessionClient.subscribe(['login'], (result) => {
console.log('User logged in:', result.data);
});
// Perform login
await sessionClient.logIn({ authProtocol: 'password', username: 'user@example.com', password: 'password' });
Relates to
SessionClientProvider: The client instance must be passed to this provider (typically viaProviderComposer) to be available via hooks.ProviderComposer: The standard way to bootstrapSessionClientProvideralongside other core providers in theAppcomponent.AuthAdapter:SessionClientdelegates actual network calls and token storage to an adapter (e.g.,CookieAuthAdapter).- Hooks:
useUser,useLogin, anduseLogOutall use theSessionClientinstance internally. DataClient:SessionClientintegrates with theDataClientto cache user data under thesession-userkey.