refactor: remove localStorage persistence and redundant refresh token logic from token store
This commit is contained in:
@@ -2,38 +2,14 @@ export type StoredTokens = {
|
|||||||
access_token: string;
|
access_token: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
const LS_KEY = "uhm_auth_tokens_v1";
|
|
||||||
|
|
||||||
let cached: StoredTokens | null = null;
|
let cached: StoredTokens | null = null;
|
||||||
|
|
||||||
function safeParseTokens(raw: string | null): StoredTokens | null {
|
|
||||||
if (!raw) return null;
|
|
||||||
try {
|
|
||||||
const v = JSON.parse(raw) as Partial<StoredTokens>;
|
|
||||||
if (!v || typeof v !== "object") return null;
|
|
||||||
if (typeof v.access_token !== "string") return null;
|
|
||||||
if (!v.access_token.trim()) return null;
|
|
||||||
return { access_token: v.access_token };
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getStoredTokens(): StoredTokens | null {
|
export function getStoredTokens(): StoredTokens | null {
|
||||||
if (cached) return cached;
|
|
||||||
if (typeof window === "undefined") return null;
|
|
||||||
cached = safeParseTokens(window.localStorage.getItem(LS_KEY));
|
|
||||||
return cached;
|
return cached;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setStoredTokens(tokens: StoredTokens | null): void {
|
export function setStoredTokens(tokens: StoredTokens | null): void {
|
||||||
cached = tokens;
|
cached = tokens;
|
||||||
if (typeof window === "undefined") return;
|
|
||||||
if (!tokens) {
|
|
||||||
window.localStorage.removeItem(LS_KEY);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
window.localStorage.setItem(LS_KEY, JSON.stringify(tokens));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAccessToken(): string | null {
|
export function getAccessToken(): string | null {
|
||||||
@@ -47,11 +23,6 @@ export function clearStoredTokens(): void {
|
|||||||
// Helper for dealing with CommonResponse where token payload shape is not strictly typed.
|
// Helper for dealing with CommonResponse where token payload shape is not strictly typed.
|
||||||
export function extractTokensFromResponsePayload(payload: any): StoredTokens | null {
|
export function extractTokensFromResponsePayload(payload: any): StoredTokens | null {
|
||||||
const data = payload?.data ?? payload;
|
const data = payload?.data ?? payload;
|
||||||
// Common shapes observed in various backends:
|
|
||||||
// - { status: true, data: { access_token, refresh_token } }
|
|
||||||
// - { data: { tokens: { access_token, refresh_token } } }
|
|
||||||
// - { data: { token: <access>, refresh_token } }
|
|
||||||
// - { accessToken, refreshToken }
|
|
||||||
const tokenContainer = data?.tokens ?? data?.token_set ?? data;
|
const tokenContainer = data?.tokens ?? data?.token_set ?? data;
|
||||||
|
|
||||||
const access =
|
const access =
|
||||||
@@ -62,11 +33,6 @@ export function extractTokensFromResponsePayload(payload: any): StoredTokens | n
|
|||||||
tokenContainer?.jwt ??
|
tokenContainer?.jwt ??
|
||||||
null;
|
null;
|
||||||
|
|
||||||
const refresh =
|
|
||||||
tokenContainer?.refresh_token ??
|
|
||||||
tokenContainer?.refreshToken ??
|
|
||||||
tokenContainer?.refresh ??
|
|
||||||
null;
|
|
||||||
if (typeof access === "string" && access.trim()) {
|
if (typeof access === "string" && access.trim()) {
|
||||||
return { access_token: access };
|
return { access_token: access };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user