mirror of
https://github.com/Tria-plc/emaui.git
synced 2026-08-30 01:48:13 +00:00
feat: migrate authentication storage to use js-cookie for improved token management
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
const MAX_AGE = 60 * 60 * 24 * 7; // ponytail: cookie lifetime knob; JWT expiry + refresh govern real auth
|
||||
import Cookies from 'js-cookie';
|
||||
|
||||
const COOKIE_EXPIRES_DAYS = 7; // ponytail: cookie lifetime knob; JWT expiry + refresh govern real auth
|
||||
|
||||
interface Backend {
|
||||
get(k: string): string | null;
|
||||
@@ -13,27 +15,25 @@ const localBackend: Backend = {
|
||||
};
|
||||
|
||||
const cookieBackend: Backend = {
|
||||
get: (k) => {
|
||||
const m = document.cookie.match(new RegExp('(?:^|;\\s*)' + k + '=([^;]*)'));
|
||||
return m ? decodeURIComponent(m[1]) : null;
|
||||
},
|
||||
get: (k) => Cookies.get(k) ?? null,
|
||||
// ponytail: 4KB/cookie cap — auth-user/current-profile JSON must stay under it
|
||||
set: (k, v) => {
|
||||
document.cookie = `${k}=${encodeURIComponent(v)}; path=/; Secure; SameSite=Strict; Max-Age=${MAX_AGE}`;
|
||||
},
|
||||
remove: (k) => {
|
||||
document.cookie = `${k}=; path=/; Secure; SameSite=Strict; Max-Age=0`;
|
||||
},
|
||||
set: (k, v) =>
|
||||
Cookies.set(k, v, { path: '/', secure: true, sameSite: 'strict', expires: COOKIE_EXPIRES_DAYS }),
|
||||
remove: (k) => Cookies.remove(k, { path: '/' }),
|
||||
};
|
||||
|
||||
import Cookies from "js-cookie";
|
||||
const AUTH_KEY_NAMES = ['auth-token', 'refresh-token', 'auth-user', 'profile-id', 'current-profile'];
|
||||
|
||||
let _prefix = "ema-auth";
|
||||
let _prefix = 'ema-auth';
|
||||
let backend: Backend = localBackend;
|
||||
|
||||
export function configureAuthStorage(prefix: string, useCookies = false) {
|
||||
_prefix = prefix;
|
||||
backend = useCookies ? cookieBackend : localBackend;
|
||||
if (useCookies) {
|
||||
// one-time cleanup: drop pre-migration localStorage leftovers so tokens live in cookies only
|
||||
AUTH_KEY_NAMES.forEach((k) => localStorage.removeItem(`${prefix}-${k}`));
|
||||
}
|
||||
}
|
||||
|
||||
function key(k: string) {
|
||||
@@ -68,16 +68,14 @@ export const authStorage = {
|
||||
[key('auth-token'), key('refresh-token'), key('auth-user'), key('profile-id'), key('current-profile')].forEach((k) =>
|
||||
backend.remove(k),
|
||||
);
|
||||
document.cookie =
|
||||
"auth-token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 UTC; SameSite=Lax";
|
||||
Cookies.remove('auth-token', { path: '/' });
|
||||
},
|
||||
};
|
||||
|
||||
export const clearAllCookies = () => {
|
||||
const cookies = Cookies.get();
|
||||
|
||||
Object.keys(cookies).forEach((name) => {
|
||||
Cookies.remove(name);
|
||||
Cookies.remove(name, { path: "/" });
|
||||
Cookies.remove(name, { path: '/' });
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user