Polish settings and auth UX
This commit is contained in:
@@ -1,27 +1,74 @@
|
||||
export const AUTH_TOKEN_KEY = "authToken";
|
||||
export const AUTH_REMEMBER_ME_KEY = "authRememberMe";
|
||||
const LEGACY_AUTH_TOKEN_KEY = "googleIdToken";
|
||||
|
||||
function getStoredToken(storage: Storage): string | null {
|
||||
try {
|
||||
return storage.getItem(AUTH_TOKEN_KEY);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function getRememberMePref(): boolean {
|
||||
try {
|
||||
return window.localStorage.getItem(AUTH_REMEMBER_ME_KEY) === "1";
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function setRememberMePref(value: boolean) {
|
||||
try {
|
||||
window.localStorage.setItem(AUTH_REMEMBER_ME_KEY, value ? "1" : "0");
|
||||
} catch {
|
||||
// ignore storage failures
|
||||
}
|
||||
}
|
||||
|
||||
export function getAuthToken(): string | null {
|
||||
const current = window.localStorage.getItem(AUTH_TOKEN_KEY);
|
||||
const current = getStoredToken(window.localStorage) || getStoredToken(window.sessionStorage);
|
||||
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);
|
||||
const legacy = window.localStorage.getItem(LEGACY_AUTH_TOKEN_KEY) || window.sessionStorage.getItem(LEGACY_AUTH_TOKEN_KEY);
|
||||
if (legacy) {
|
||||
window.localStorage.setItem(AUTH_TOKEN_KEY, legacy);
|
||||
window.localStorage.removeItem(LEGACY_AUTH_TOKEN_KEY);
|
||||
const remember = getRememberMePref();
|
||||
setAuthToken(legacy, { remember });
|
||||
try {
|
||||
window.localStorage.removeItem(LEGACY_AUTH_TOKEN_KEY);
|
||||
window.sessionStorage.removeItem(LEGACY_AUTH_TOKEN_KEY);
|
||||
} catch {
|
||||
// ignore storage failures
|
||||
}
|
||||
return legacy;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function setAuthToken(token: string) {
|
||||
window.localStorage.setItem(AUTH_TOKEN_KEY, token);
|
||||
export function setAuthToken(token: string, options?: { remember?: boolean }) {
|
||||
const remember = options?.remember ?? getRememberMePref();
|
||||
try {
|
||||
if (remember) {
|
||||
window.localStorage.setItem(AUTH_TOKEN_KEY, token);
|
||||
window.sessionStorage.removeItem(AUTH_TOKEN_KEY);
|
||||
} else {
|
||||
window.sessionStorage.setItem(AUTH_TOKEN_KEY, token);
|
||||
window.localStorage.removeItem(AUTH_TOKEN_KEY);
|
||||
}
|
||||
} catch {
|
||||
window.localStorage.setItem(AUTH_TOKEN_KEY, token);
|
||||
}
|
||||
}
|
||||
|
||||
export function clearAuthToken() {
|
||||
window.localStorage.removeItem(AUTH_TOKEN_KEY);
|
||||
try {
|
||||
window.localStorage.removeItem(AUTH_TOKEN_KEY);
|
||||
window.sessionStorage.removeItem(AUTH_TOKEN_KEY);
|
||||
} catch {
|
||||
// ignore storage failures
|
||||
}
|
||||
}
|
||||
|
||||
export function decodeJwtPayload(token: string): any {
|
||||
|
||||
Reference in New Issue
Block a user