|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const AUTH_KEYS = { |
|
|
ACCESS_TOKEN: "access_token", |
|
|
REFRESH_TOKEN: "refresh_token", |
|
|
USER: "user", |
|
|
} as const |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function setAuthData(accessToken: string, refreshToken?: string, user?: any) { |
|
|
if (typeof window === "undefined") return |
|
|
|
|
|
localStorage.setItem(AUTH_KEYS.ACCESS_TOKEN, accessToken) |
|
|
|
|
|
if (refreshToken) { |
|
|
localStorage.setItem(AUTH_KEYS.REFRESH_TOKEN, refreshToken) |
|
|
} |
|
|
|
|
|
if (user) { |
|
|
localStorage.setItem(AUTH_KEYS.USER, JSON.stringify(user)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getAccessToken(): string | null { |
|
|
if (typeof window === "undefined") return null |
|
|
return localStorage.getItem(AUTH_KEYS.ACCESS_TOKEN) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getRefreshToken(): string | null { |
|
|
if (typeof window === "undefined") return null |
|
|
return localStorage.getItem(AUTH_KEYS.REFRESH_TOKEN) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getUser(): any | null { |
|
|
if (typeof window === "undefined") return null |
|
|
|
|
|
const userStr = localStorage.getItem(AUTH_KEYS.USER) |
|
|
if (!userStr) return null |
|
|
|
|
|
try { |
|
|
return JSON.parse(userStr) |
|
|
} catch { |
|
|
return null |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function clearAuthData() { |
|
|
if (typeof window === "undefined") return |
|
|
|
|
|
localStorage.removeItem(AUTH_KEYS.ACCESS_TOKEN) |
|
|
localStorage.removeItem(AUTH_KEYS.REFRESH_TOKEN) |
|
|
localStorage.removeItem(AUTH_KEYS.USER) |
|
|
localStorage.removeItem("know_rights_user_details") |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isAuthenticated(): boolean { |
|
|
return getAccessToken() !== null |
|
|
} |
|
|
|