Files
co-lite/solvway-admin-portal/app/lib/routeGuard.ts
T
ngqileandClaude Opus 5.5 bb2c5c5db7 feat(portal): landing pages list, editor, preview and public page
Pages list and Tiptap editor (ported from babyboo) with image upload on
save, the FORM event picker, field errors at their fields, delete
confirmation, unsaved-edits guard and a draft preview. The public page
/p/<company>/<slug> renders on the server with metadata, sanitize-html,
the company's logo and accent, and the sign-up form with UTM tags.
Companies get branding; the event lead detail shows the web page and
campaign. Labels in English and Chinese.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
2026-12-02 16:30:54 +08:00

38 lines
1.6 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"]);
// Visitor pages, not signed in: /r/<token> QR self-registration, /i/<token> personal invite, /c/<token> event check-in.
// (A pattern, so PUBLIC_PATHS stays exact.)
const VISITOR_PATH = /^\/[ric]\/[^/]+$/;
// Published landing pages: /p/<company-code>/<slug>.
const LANDING_PATH = /^\/p\/[^/]+\/[^/]+$/;
export function isPublicPath(pathname: string): boolean {
return PUBLIC_PATHS.has(pathname) || VISITOR_PATH.test(pathname) || LANDING_PATH.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 && !VISITOR_PATH.test(pathname) && pathname !== "/reset-password" && pathname !== "/login") {
return "/reset-password";
}
return null;
}