@teamhanko/hanko-frontend-sdk
Version:
A package for simplifying UI integration with the Hanko API. It is meant for use in browsers only.
81 lines (80 loc) • 3.41 kB
TypeScript
import { Listener } from "./Listener";
import { Dispatcher } from "./Dispatcher";
import { HankoOptions } from "../../Hanko";
/**
* A class that manages session checks, dispatches events based on session status,
* and uses broadcast channels for inter-tab communication.
*
* @category SDK
* @subcategory Internal
* @extends Dispatcher
* @param {string} api - The API endpoint URL.
* @param {HankoOptions} options - The internal configuration options of the SDK.
*/
export declare class Relay extends Dispatcher {
listener: Listener;
private readonly checkInterval;
private readonly client;
private readonly sessionState;
private readonly windowActivityManager;
private readonly scheduler;
private readonly sessionChannel;
private isLoggedIn;
constructor(api: string, options: HankoOptions);
/**
* Sets up all event listeners and initializes session management.
* This method is crucial for ensuring the session is monitored across all tabs.
* @private
*/
private initializeEventListeners;
/**
* Initiates session checking based on the last check time.
* This method decides when the next check should occur to balance between performance and freshness.
* @private
*/
private startSessionCheck;
/**
* Validates the current session and updates session information.
* This method checks if the session is still valid and updates local data accordingly.
* @returns {Promise<SessionCheckResult>} - A promise that resolves with the session check result.
* @private
*/
private checkSession;
/**
* Resets session-related states when a session expires.
* Ensures that authentication state is cleared and an expiration event is dispatched.
* Assumes the user is logged out by default if the session state is unknown.
* @private
*/
private onSessionExpired;
/**
* Handles session expired events from broadcast messages.
* @private
*/
private onChannelSessionExpired;
/**
* Handles session creation events from broadcast messages.
* @param {BroadcastMessage} msg - The broadcast message containing session details.
* @private
*/
private onChannelSessionCreated;
/**
* Handles leadership requests from other tabs.
* @private
*/
private onChannelLeadershipRequested;
/**
* Retrieves or generates the session check channel name based on the session token storage location.
*
* - If the `sessionTokenLocation` is `"cookie"`, the provided `sessionCheckChannelName` is returned as-is.
* - If the `sessionTokenLocation` is `"sessionStorage"`, the function attempts to retrieve the channel name from
* `sessionStorage`. If none is found, a new name is generated with the value of `sessionCheckChannelName` as a prefix and a random number,
* then stored in `sessionStorage` for future use.
*
* @param sessionTokenLocation - Indicates where the session token is stored, either `"cookie"` or `"sessionStorage"`.
* @param sessionCheckChannelName - The name or prefix used for the session check channel.
* @returns The resolved session check channel name, or `undefined` if not applicable.
* @private
*/
private getSessionCheckChannelName;
}