authvisage-sdk
Version:
authvisage client sdk
109 lines (103 loc) • 3.61 kB
TypeScript
import z from 'zod';
declare const clientOptionsSchema: z.ZodObject<{
projectId: z.ZodString;
platformUrl: z.ZodEffects<z.ZodString, string, string>;
backendUrl: z.ZodEffects<z.ZodString, string, string>;
redirectUrl: z.ZodEffects<z.ZodString, string, string>;
}, "strict", z.ZodTypeAny, {
projectId: string;
platformUrl: string;
backendUrl: string;
redirectUrl: string;
}, {
projectId: string;
platformUrl: string;
backendUrl: string;
redirectUrl: string;
}>;
type ClientOptions = z.infer<typeof clientOptionsSchema>;
interface TokenResponse {
access_token: string;
refresh_token?: string;
expires_in?: number;
}
interface User {
id: string;
email: string;
fullname: string;
}
type Callback = (user: User | null) => void;
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 | null>;
/**
* 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;
}
/**
* Main AuthVisage client for handling authentication
*/
declare class AuthVisageClient {
private readonly projectId;
private readonly platformUrl;
private readonly backendUrl;
private readonly redirectUrl;
readonly auth: TokenManager;
private initialized;
constructor(options: ClientOptions);
/**
* Get Session id from the backend
* @returns Session id
*/
private _getSessionId;
/**
* Constructs the OAuth authorization URL
*/
private _constructAuthUrl;
/**
* Handles the OAuth callback and exchanges the authorization code for an access token
*/
private _handleOAuthCallback;
/**
* Initiates the face login process by redirecting the user to AuthVisage
*/
faceLogin(): Promise<void>;
}
export { AuthVisageClient, type Callback, type ClientOptions, TokenManager, type TokenResponse, type User };