Skip to main content
Manages the authentication state and access token for standard CDP authentication (email/SMS/OAuth flows).

Implements

Constructors

Constructor

new AuthManager(projectId: string): AuthManager;
Initializes the token manager.

Parameters

ParameterTypeDescription
projectIdstringThe project ID.

Returns

AuthManager

Methods

getUser()

getUser(): 
  | null
  | User;
Gets the current user, or null if there is no user signed in.

Returns

| null | User The current user.

Implementation of

IAuthManager.getUser

isSignedIn()

isSignedIn(): Promise<boolean>;
Returns whether the user is signed in - i.e., whether there is an unexpired access token and user. Attempts to refresh the token if it’s expired.

Returns

Promise<boolean> True if the user is signed in, false otherwise.

Implementation of

IAuthManager.isSignedIn

signOut()

signOut(): Promise<void>;
Signs out the user, clearing all authentication state.

Returns

Promise<void>

Implementation of

IAuthManager.signOut

addAuthStateChangeCallback()

addAuthStateChangeCallback(callback: OnAuthStateChangeFn): void;
Adds a callback to be called when the auth state changes.

Parameters

ParameterTypeDescription
callbackOnAuthStateChangeFnThe function to call when the auth state changes.

Returns

void

Implementation of

IAuthManager.addAuthStateChangeCallback

getToken()

getToken(options?: {
  forceRefresh?: boolean;
}): Promise<null | string>;
Gets the access token, refreshing it if it is expired. Returns null if the user is not signed in.

Parameters

ParameterTypeDescription
options?{ forceRefresh?: boolean; }The options for getting the token.
options.forceRefresh?booleanWhether to force a refresh of the token.

Returns

Promise<null | string> The access token.

Implementation of

IAuthManager.getToken

getTokenExpiration()

getTokenExpiration(): Promise<null | number>;
Gets the expiration time of the access token, or null if the user is not signed in.

Returns

Promise<null | number> The expiration time of the access token.

Implementation of

IAuthManager.getTokenExpiration

getWalletSecretId()

getWalletSecretId(): Promise<string>;
Gets the currently registered wallet secret ID. Rejects if the user is not signed in.

Returns

Promise<string> The wallet secret ID.

Implementation of

IAuthManager.getWalletSecretId

getXWalletAuth()

getXWalletAuth(options: {
  requestMethod: string;
  requestHost: string;
  requestPath: string;
  requestData?: Record<string, unknown>;
}): Promise<string>;
Gets the X-Wallet-Auth header. Rejects if the user is not signed in.

Parameters

ParameterTypeDescription
options{ requestMethod: string; requestHost: string; requestPath: string; requestData?: Record<string, unknown>; }The options for the request.
options.requestMethodstringThe HTTP method of the request.
options.requestHoststringThe host of the request.
options.requestPathstringThe path of the request.
options.requestData?Record<string, unknown>The data of the request.

Returns

Promise<string> The X-Wallet-Auth header.

Implementation of

IAuthManager.getXWalletAuth

getAuthState()

getAuthState(): 
  | null
  | AuthState;
Gets the authentication state.

Returns

| null | AuthState The authentication state.

Implementation of

IAuthManager.getAuthState

setAuthState()

setAuthState(authState: AuthState): Promise<void>;
Sets the authentication state.

Parameters

ParameterTypeDescription
authStateAuthStateThe authentication state.

Returns

Promise<void>

Implementation of

IAuthManager.setAuthState

clearAuthState()

clearAuthState(): Promise<void>;
Clears the authentication state.

Returns

Promise<void>

Implementation of

IAuthManager.clearAuthState

ensureInitialized()

ensureInitialized(): Promise<void>;
Ensures the AuthManager is initialized before proceeding. If initialization is already in progress, waits for it to complete.

Returns

Promise<void>

Implementation of

IAuthManager.ensureInitialized

_doInitialize()

protected _doInitialize(): Promise<void>;
Internal async initialization logic.

Returns

Promise<void>

shouldRefreshToken()

protected shouldRefreshToken(): boolean;
Returns whether the token should be refreshed.

Returns

boolean True if the token should be refreshed, false otherwise.

refreshAccessToken()

protected refreshAccessToken(): Promise<void>;
Refreshes the access token and transitions the auth state accordingly. If a refresh is already in progress, it will wait for that refresh to complete.

Returns

Promise<void> The new access token.

scheduleTokenRefresh()

protected scheduleTokenRefresh(): void;
Schedules a token refresh to occur exactly when shouldRefreshToken() would return true. Uses the same REFRESH_CREDENTIALS_BUFFER_MS timing as the rest of the auth system.

Returns

void

cancelTokenRefresh()

protected cancelTokenRefresh(): void;
Cancels any scheduled token refresh.

Returns

void

Properties

PropertyModifierTypeDefault value
projectIdprotectedstringundefined
authStateprotected| null | AuthStatenull
walletSecretprotected| null | WalletSecretnull
authStateChangeCallbacksprotectedOnAuthStateChangeFn[][]
initPromiseprotectednull | Promise<void>null
refreshTimeoutprotectednull | Timeoutnull