Decode JWT client-side instead of calling /api/auth/me
- Add lib/auth.ts with decodeJWT() and isTokenValid() - Layout decodes JWT directly - no extra request - Domains page also uses JWT for admin check - Removed redundant user.domains from domains list page
This commit is contained in:
parent
19360e9a9f
commit
ccaa1eca7e
3 changed files with 55 additions and 60 deletions
28
frontend/src/lib/auth.ts
Normal file
28
frontend/src/lib/auth.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
interface JWTPayload {
|
||||
userId: number;
|
||||
username: string;
|
||||
role: string;
|
||||
exp: number;
|
||||
iat: number;
|
||||
}
|
||||
|
||||
export function decodeJWT(token: string): JWTPayload | null {
|
||||
try {
|
||||
const parts = token.split('.');
|
||||
if (parts.length !== 3) return null;
|
||||
|
||||
const payload = parts[1];
|
||||
const decoded = atob(payload.replace(/-/g, '+').replace(/_/g, '/'));
|
||||
return JSON.parse(decoded);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function isTokenValid(token: string): boolean {
|
||||
const payload = decodeJWT(token);
|
||||
if (!payload) return false;
|
||||
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
return payload.exp > now;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue