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 { clearAuth, readAuth, writeAuth } from './tokenStorage';
|
||||||
import { hasAdminAccess, hasStaffAccess, hasAdminPanelAccess } from './roleAccess';
|
import { hasAdminAccess, hasStaffAccess, hasAdminPanelAccess } from './roleAccess';
|
||||||
import { hasPermission as checkPermission } from './permissions';
|
import { hasPermission as checkPermission } from './permissions';
|
||||||
import { ensureFreshAccessToken, isAccessTokenExpiringSoon, logoutSession } from '../services/api';
|
import {
|
||||||
|
ensureFreshAccessToken,
|
||||||
|
isAccessTokenExpiringSoon,
|
||||||
|
logoutSession,
|
||||||
|
readJwtPayload,
|
||||||
|
} from '../services/api';
|
||||||
|
|
||||||
export type AuthState = {
|
export type AuthState = {
|
||||||
accessToken: string | null;
|
accessToken: string | null;
|
||||||
@ -53,7 +58,22 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|||||||
const hadSession = Boolean(readAuth().refreshToken || readAuth().accessToken);
|
const hadSession = Boolean(readAuth().refreshToken || readAuth().accessToken);
|
||||||
if (!sessionNeedsNetworkRefresh()) {
|
if (!sessionNeedsNetworkRefresh()) {
|
||||||
if (!cancelled) {
|
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);
|
setAuthReady(true);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|||||||
@ -67,19 +67,36 @@ api.interceptors.request.use((config) => {
|
|||||||
|
|
||||||
let refreshing: Promise<string | null> | null = null;
|
let refreshing: Promise<string | null> | null = null;
|
||||||
|
|
||||||
/** Returns JWT `exp` (unix seconds) or null if the token is not a readable JWT. */
|
/** Decode JWT payload (browser); returns null if unreadable. */
|
||||||
export function readJwtExpSeconds(accessToken: string): number | null {
|
export function readJwtPayload(accessToken: string): {
|
||||||
|
exp?: number;
|
||||||
|
role?: string;
|
||||||
|
roles?: string[];
|
||||||
|
permissions?: string[];
|
||||||
|
sub?: string;
|
||||||
|
} | null {
|
||||||
try {
|
try {
|
||||||
const parts = accessToken.split('.');
|
const parts = accessToken.split('.');
|
||||||
if (parts.length < 2) return null;
|
if (parts.length < 2) return null;
|
||||||
const json = atob(parts[1]!.replace(/-/g, '+').replace(/_/g, '/'));
|
const json = atob(parts[1]!.replace(/-/g, '+').replace(/_/g, '/'));
|
||||||
const payload = JSON.parse(json) as { exp?: unknown };
|
return JSON.parse(json) as {
|
||||||
return typeof payload.exp === 'number' ? payload.exp : null;
|
exp?: number;
|
||||||
|
role?: string;
|
||||||
|
roles?: string[];
|
||||||
|
permissions?: string[];
|
||||||
|
sub?: string;
|
||||||
|
};
|
||||||
} catch {
|
} catch {
|
||||||
return null;
|
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. */
|
/** True when the access token is missing, malformed, expired, or within `skewSeconds` of expiry. */
|
||||||
export function isAccessTokenExpiringSoon(accessToken: string | null, skewSeconds = 60): boolean {
|
export function isAccessTokenExpiringSoon(accessToken: string | null, skewSeconds = 60): boolean {
|
||||||
if (!accessToken) return true;
|
if (!accessToken) return true;
|
||||||
@ -100,10 +117,16 @@ async function refreshAccessToken(): Promise<string | null> {
|
|||||||
const accessToken = res.data?.accessToken;
|
const accessToken = res.data?.accessToken;
|
||||||
if (accessToken) {
|
if (accessToken) {
|
||||||
const prev = readAuth();
|
const prev = readAuth();
|
||||||
|
const claims = readJwtPayload(accessToken);
|
||||||
writeAuth({
|
writeAuth({
|
||||||
...prev,
|
...prev,
|
||||||
accessToken,
|
accessToken,
|
||||||
refreshToken: res.data.refreshToken ?? prev.refreshToken,
|
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;
|
return accessToken;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user