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

config
object

An optional configuration object.

1 subfields

Returns

string
The session token, or an empty string if not found.

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

value
string
required
The session token value to store.

Returns

void
This method does not return a value.

Example

sdk.token.setSessionToken('new-session-token-value-from-server');

removeSessionToken#

Removes the session token from the browser's cookies.

Returns

void
This method does not return a value.

Example

sdk.token.removeSessionToken();

getRefreshToken#

Retrieves the refresh token from the browser's localStorage.

Returns

string | null
The refresh token, or null if it does not exist.

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

value
string
required
The refresh token value to store.

Returns

void
This method does not return a value.

Example

sdk.token.setRefreshToken('new-refresh-token-value-from-server');

removeRefreshToken#

Removes the refresh token from the browser's localStorage.

Returns

void
This method does not return a value.

Example

sdk.token.removeRefreshToken();