feat: update authentication storage to use cookies and improve token retrieval

This commit is contained in:
estifanos
2026-07-22 09:31:10 +00:00
parent abcb538e4f
commit 75220e2b9a
6 changed files with 70 additions and 39 deletions

View File

@@ -11,13 +11,22 @@ const TOKEN_STORAGE_KEYS = [
'auth-token',
] as const;
function getCookie(name: string): string | undefined {
const match = document.cookie.match(
new RegExp('(?:^|;\\s*)' + name + '=([^;]*)'),
);
return match ? decodeURIComponent(match[1]) : undefined;
}
export function resolveTokenFromStorage(): string | undefined {
// cookie first (backoffice stores tokens there), then localStorage (portal / legacy)
for (const key of TOKEN_STORAGE_KEYS) {
const cookie = getCookie(key);
if (cookie) return cookie;
const stored = localStorage.getItem(key);
if (stored) return stored;
}
const match = document.cookie.match(/(?:^|;\s*)auth-token=([^;]*)/);
return match ? decodeURIComponent(match[1]) : undefined;
return undefined;
}
export function resolveSessionContext(state?: { auth?: { token?: string } }): {