authvisage-sdk
Version:
authvisage client sdk
47 lines • 2.05 kB
TypeScript
import type { User, TokenResponse } from "@/types";
export type Callback = (user: User | null) => void;
export declare class TokenManager {
private backendUrl;
private expirationTimer;
private listenerManager;
constructor(backendUrl: string);
/**
* Handle token expiration by setting a timer.
* @param expiresIn - The time in seconds until the token expires.
*/
private _handleTokenExpiration;
/**
* Initializes the user session by validating and decoding the access token, notifying registered listeners,
* and scheduling automatic token expiration handling.
*
* @param session - The token response containing the `access_token` and `expires_in` values.
* @throws {Error} If the session does not include an `access_token`.
* @returns A promise that resolves once the session is set and expiration handling is in place.
*/
setSession(session: TokenResponse): Promise<void>;
/**
* Sends a refresh request to get a new access token.
* Assumes the refresh token is stored in cookies.
*/
getAccessToken(): Promise<string>;
/**
* Logs the current user out by sending a POST request to the backend logout endpoint.
*
* This method includes credentials with the request and throws an error if the response
* status is not in the 200–299 range. On a successful logout, it notifies all registered
* listeners with `null`.
*
* @returns A promise that resolves when the logout operation completes successfully.
* @throws {Error} If the logout request fails or the response is not OK.
*/
logout(): Promise<void>;
/**
* Subscribes to authentication state changes.
*
* @param callback - Function to be invoked whenever the authentication state updates.
* @returns A Promise that resolves to an unsubscribe function which,
* when called, removes the listener.
*/
onAuthStateChange(callback: Callback): () => void;
}
//# sourceMappingURL=tokenManager.d.ts.map