A code of the wrong shape now shows "no longer active" instead of failing, My QR codes skips events with no end date and marks switched-off codes, and a session waiting on a password reset can still open a QR page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
33 lines
1.4 KiB
TypeScript
33 lines
1.4 KiB
TypeScript
// Pure, edge-safe route-guard decision logic, shared by middleware.
|
|
// Returns the path to redirect to, or null to allow the request.
|
|
// Ported from solvway-customer-portal's app/lib/routeGuard.ts, trimmed to the
|
|
// admin portal's session shape.
|
|
|
|
// /forgot-password is the signed-out "email me a temporary password" form.
|
|
// /reset-password is the first sign-in set-password form (login answered
|
|
// resetPasswordRequired); it is reached right after login, so it stays public.
|
|
export const PUBLIC_PATHS = new Set<string>(["/login", "/reset-password", "/forgot-password"]);
|
|
|
|
// /r/<token> is the QR self-registration page: visitors are not signed in. (A prefix, so PUBLIC_PATHS stays exact.)
|
|
export function isPublicPath(pathname: string): boolean {
|
|
return PUBLIC_PATHS.has(pathname) || /^\/r\/[^/]+$/.test(pathname);
|
|
}
|
|
|
|
export interface SessionState {
|
|
present: boolean;
|
|
/** The session's first-sign-in password reset is still pending. */
|
|
resetRequired?: boolean;
|
|
}
|
|
|
|
export function guardDecision(pathname: string, session: SessionState | null): string | null {
|
|
const present = !!session?.present;
|
|
if (!present) {
|
|
return isPublicPath(pathname) ? null : "/login";
|
|
}
|
|
// A pending reset confines the session to the set-password form (/login stays reachable).
|
|
if (session?.resetRequired && !/^\/r\/[^/]+$/.test(pathname) && pathname !== "/reset-password" && pathname !== "/login") {
|
|
return "/reset-password";
|
|
}
|
|
return null;
|
|
}
|