38 lines
1.0 KiB
TypeScript
38 lines
1.0 KiB
TypeScript
export const AUTH_TOKEN_KEY = "authToken";
|
|
const LEGACY_AUTH_TOKEN_KEY = "googleIdToken";
|
|
|
|
export function getAuthToken(): string | null {
|
|
const current = window.localStorage.getItem(AUTH_TOKEN_KEY);
|
|
if (current) return current;
|
|
|
|
// Backward compat for older builds that stored Google ID tokens under a different key.
|
|
const legacy = window.localStorage.getItem(LEGACY_AUTH_TOKEN_KEY);
|
|
if (legacy) {
|
|
window.localStorage.setItem(AUTH_TOKEN_KEY, legacy);
|
|
window.localStorage.removeItem(LEGACY_AUTH_TOKEN_KEY);
|
|
return legacy;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
export function setAuthToken(token: string) {
|
|
window.localStorage.setItem(AUTH_TOKEN_KEY, token);
|
|
}
|
|
|
|
export function clearAuthToken() {
|
|
window.localStorage.removeItem(AUTH_TOKEN_KEY);
|
|
}
|
|
|
|
export function decodeJwtPayload(token: string): any {
|
|
try {
|
|
const parts = token.split(".");
|
|
if (parts.length < 2) return null;
|
|
const base64 = parts[1].replaceAll("-", "+").replaceAll("_", "/");
|
|
const json = atob(base64);
|
|
return JSON.parse(json);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|