TokenService
The TokenService provides a low-level API for direct interaction with session and refresh tokens. It handles the logic of storing and retrieving these tokens from their respective browser storage locations: cookies for session tokens and localStorage for refresh tokens.
While this service is available for specific use cases, most developers will not need to use it directly. Higher-level abstractions like the AuthService and the createAxios/createFetch helpers manage token handling automatically. For typical authentication and session management, please refer to the Authentication Guide.
Methods#
getSessionToken#
Retrieves the current session token. The service first attempts to get the token from browser cookies. As a fallback, it can check localStorage if a specific key is provided in the configuration.
Parameters
An optional configuration object.
Returns
Example
// Primarily checks cookies
const sessionToken = sdk.token.getSessionToken();
// Checks cookies, then localStorage with a specific key
const sessionTokenWithFallback = sdk.token.getSessionToken({
sessionTokenKey: 'my-app-session-key',
});setSessionToken#
Sets or updates the session token in the browser's cookies.
Parameters
Returns
Example
sdk.token.setSessionToken('new-session-token-value-from-server');removeSessionToken#
Removes the session token from the browser's cookies.
Returns
Example
sdk.token.removeSessionToken();getRefreshToken#
Retrieves the refresh token from the browser's localStorage.
Returns
Example
const refreshToken = sdk.token.getRefreshToken();
if (refreshToken) {
console.log('Refresh token is available.');
}setRefreshToken#
Sets or updates the refresh token in the browser's localStorage.
Parameters
Returns
Example
sdk.token.setRefreshToken('new-refresh-token-value-from-server');removeRefreshToken#
Removes the refresh token from the browser's localStorage.
Returns
Example
sdk.token.removeRefreshToken();