UNPKG

@auth0/auth0-spa-js

Version:

Auth0 SDK for Single Page Applications using Authorization Code Grant Flow with PKCE

68 lines (67 loc) • 2.97 kB
import type { AnonymousSessionClient, AnonymousSession, CreateAnonymousSessionOptions, GetAnonymousAccessTokenOptions } from '@auth0/auth0-auth-js'; import type { ILockManager } from '../lock'; /** * @category Tokens & Users */ export type AnonymousGetTokenSilentlyOptions = Omit<GetAnonymousAccessTokenOptions, 'sessionToken'>; export type AnonymousTokenResult = { accessToken: string; expiresAt: number; scope?: string; }; /** * Browser-layer wrapper around auth0-auth-js `AnonymousSessionClient`. * * Adds local session storage (localStorage or memory) so callers never need to * manage the session token themselves. `getTokenSilently()` returns a cached * access token when still valid and renews it transparently when expired. * * The session token is stored once and shared across all audience/scope slots. * Each slot stores only its own access token and expiry. * * Exposed on `Auth0Client` as `auth0.anonymous`. * * @category Clients */ export declare class AnonymousSessionApiClient { private authJsClient; private readonly cache; private readonly lockManager; private readonly clientId; constructor(authJsClient: AnonymousSessionClient, clientId: string, cacheMode?: 'localStorage' | 'memory', lockManager?: ILockManager); /** * Creates a new anonymous session and persists the tokens locally. */ createSession(options?: CreateAnonymousSessionOptions): Promise<AnonymousSession>; /** * Returns a valid anonymous access token, renewing it when expired. * * If the stored access token is still fresh (more than 60 s remaining), it is * returned directly without a network call. Otherwise the session token is used * to re-mint the access token. If the session token has also expired, the local * cache is cleared and an `AnonymousSessionError` with code `session_expired` is * thrown — call `createSession()` to start a new session. * * @throws {AnonymousSessionError} with code `session_expired` if the session token has expired. */ getTokenSilently(options?: AnonymousGetTokenSilentlyOptions): Promise<AnonymousTokenResult>; /** * Mints a short-lived transfer ticket (30s single-use JWE) for linking the * anonymous session during `loginWithRedirect()`. Returns `null` if no session * is active or the request fails — callers must never be blocked from login. */ mintTransferToken(): Promise<string | null>; /** * Ends the anonymous session and clears all locally stored tokens. */ logout(): Promise<void>; /** * Returns true if a session token exists in the local cache. */ hasSession(): boolean; /** * Always returns `null` in EA — session tokens are issued as opaque JWEs and * cannot be decoded client-side. Reserved for future JWT format support. */ getClaims(): null; }