After configuring VPS health1
This commit is contained in:
parent
a7db8846f4
commit
d4fe4793fb
@ -6,7 +6,12 @@ import { Box, CircularProgress } from '@mui/material';
|
||||
import { clearAuth, readAuth, writeAuth } from './tokenStorage';
|
||||
import { hasAdminAccess, hasStaffAccess, hasAdminPanelAccess } from './roleAccess';
|
||||
import { hasPermission as checkPermission } from './permissions';
|
||||
import { ensureFreshAccessToken, isAccessTokenExpiringSoon, logoutSession } from '../services/api';
|
||||
import {
|
||||
ensureFreshAccessToken,
|
||||
isAccessTokenExpiringSoon,
|
||||
logoutSession,
|
||||
readJwtPayload,
|
||||
} from '../services/api';
|
||||
|
||||
export type AuthState = {
|
||||
accessToken: string | null;
|
||||
@ -53,7 +58,22 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const hadSession = Boolean(readAuth().refreshToken || readAuth().accessToken);
|
||||
if (!sessionNeedsNetworkRefresh()) {
|
||||
if (!cancelled) {
|
||||
setAuthState(readAuth());
|
||||
// Prefer live JWT claims over stale localStorage permissions (new RBAC keys after deploy)
|
||||
const cur = readAuth();
|
||||
const claims = cur.accessToken ? readJwtPayload(cur.accessToken) : null;
|
||||
if (claims?.permissions) {
|
||||
const next = {
|
||||
...cur,
|
||||
role: claims.role ?? cur.role,
|
||||
roles: claims.roles?.length ? claims.roles : cur.roles,
|
||||
permissions: claims.permissions,
|
||||
userId: claims.sub ?? cur.userId,
|
||||
};
|
||||
writeAuth(next);
|
||||
setAuthState(next);
|
||||
} else {
|
||||
setAuthState(cur);
|
||||
}
|
||||
setAuthReady(true);
|
||||
}
|
||||
return;
|
||||
|
||||
@ -67,19 +67,36 @@ api.interceptors.request.use((config) => {
|
||||
|
||||
let refreshing: Promise<string | null> | null = null;
|
||||
|
||||
/** Returns JWT `exp` (unix seconds) or null if the token is not a readable JWT. */
|
||||
export function readJwtExpSeconds(accessToken: string): number | null {
|
||||
/** Decode JWT payload (browser); returns null if unreadable. */
|
||||
export function readJwtPayload(accessToken: string): {
|
||||
exp?: number;
|
||||
role?: string;
|
||||
roles?: string[];
|
||||
permissions?: string[];
|
||||
sub?: string;
|
||||
} | null {
|
||||
try {
|
||||
const parts = accessToken.split('.');
|
||||
if (parts.length < 2) return null;
|
||||
const json = atob(parts[1]!.replace(/-/g, '+').replace(/_/g, '/'));
|
||||
const payload = JSON.parse(json) as { exp?: unknown };
|
||||
return typeof payload.exp === 'number' ? payload.exp : null;
|
||||
return JSON.parse(json) as {
|
||||
exp?: number;
|
||||
role?: string;
|
||||
roles?: string[];
|
||||
permissions?: string[];
|
||||
sub?: string;
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns JWT `exp` (unix seconds) or null if the token is not a readable JWT. */
|
||||
export function readJwtExpSeconds(accessToken: string): number | null {
|
||||
const payload = readJwtPayload(accessToken);
|
||||
return typeof payload?.exp === 'number' ? payload.exp : null;
|
||||
}
|
||||
|
||||
/** True when the access token is missing, malformed, expired, or within `skewSeconds` of expiry. */
|
||||
export function isAccessTokenExpiringSoon(accessToken: string | null, skewSeconds = 60): boolean {
|
||||
if (!accessToken) return true;
|
||||
@ -100,10 +117,16 @@ async function refreshAccessToken(): Promise<string | null> {
|
||||
const accessToken = res.data?.accessToken;
|
||||
if (accessToken) {
|
||||
const prev = readAuth();
|
||||
const claims = readJwtPayload(accessToken);
|
||||
writeAuth({
|
||||
...prev,
|
||||
accessToken,
|
||||
refreshToken: res.data.refreshToken ?? prev.refreshToken,
|
||||
// Keep Options/RBAC UI in sync when catalog permissions change after deploy
|
||||
role: claims?.role ?? prev.role,
|
||||
roles: claims?.roles?.length ? claims.roles : prev.roles,
|
||||
permissions: claims?.permissions ?? prev.permissions,
|
||||
userId: claims?.sub ?? prev.userId,
|
||||
});
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user