import { TokenOptions, LoginUrlOptions } from "../types/auth"; /** * Default storage key for the access token */ const DEFAULT_STORAGE_KEY = "b44_access_token"; /** * Default parameter name for the access token in URL */ const DEFAULT_PARAM_NAME = "access_token"; /** * Get the access token from URL parameters or localStorage * @param options Token options * @returns Access token or null if not found */ export function getAccessToken(options: TokenOptions = {}): string | null { const { storageKey = DEFAULT_STORAGE_KEY, paramName = DEFAULT_PARAM_NAME, saveToStorage = true, removeFromUrl = true, } = options; let token: string | null = null; // Try to get token from URL parameters if (typeof window !== "undefined" && window.location) { try { const urlParams = new URLSearchParams(window.location.search); token = urlParams.get(paramName); if (token) { // Save token to localStorage if requested if (saveToStorage) { saveAccessToken(token, { storageKey }); } // Remove token from URL if requested if (removeFromUrl) { urlParams.delete(paramName); const newUrl = `${window.location.pathname}${urlParams.toString() ? `?${urlParams.toString()}` : ""}${window.location.hash}`; window.history.replaceState({}, document.title, newUrl); } return token; } } catch (error) { console.error("Error retrieving token from URL:", error); } } // Try to get token from localStorage if (typeof window !== "undefined" && window.localStorage) { try { token = window.localStorage.getItem(storageKey); return token; } catch (error) { console.error("Error retrieving token from localStorage:", error); } } return null; } /** * Save the access token to localStorage * @param token Access token * @param options Token options * @returns Whether the token was successfully saved */ export function saveAccessToken( token: string, options: TokenOptions = {} ): boolean { const { storageKey = DEFAULT_STORAGE_KEY } = options; if (typeof window === "undefined" || !window.localStorage || !token) { return false; } try { window.localStorage.setItem(storageKey, token); return true; } catch (error) { console.error("Error saving token to localStorage:", error); return false; } } /** * Remove the access token from localStorage * @param options Token options * @returns Whether the token was successfully removed */ export function removeAccessToken(options: TokenOptions = {}): boolean { const { storageKey = DEFAULT_STORAGE_KEY } = options; if (typeof window === "undefined" || !window.localStorage) { return false; } try { window.localStorage.removeItem(storageKey); return true; } catch (error) { console.error("Error removing token from localStorage:", error); return false; } } /** * Get the login URL for the specified return URL and configuration * @param returnUrl URL to return to after login * @param options Login URL options * @returns Login URL */ export function getLoginUrl( returnUrl: string | undefined, options: LoginUrlOptions ): string { const { serverUrl, appId, loginPath = "/login" } = options; if (!serverUrl || !appId) { throw new Error("serverUrl and appId are required to construct login URL"); } const actualReturnUrl = returnUrl || (typeof window !== "undefined" ? window.location.href : ""); return `${serverUrl}${loginPath}?from_url=${encodeURIComponent(actualReturnUrl)}&app_id=${appId}`; }