67 lines
1.6 KiB
TypeScript
67 lines
1.6 KiB
TypeScript
import type { Role } from "@public-web-portals/shared";
|
|
|
|
/** Route prefixes that require any authenticated role */
|
|
const protectedPrefixes = [
|
|
"/dashboard",
|
|
"/submit",
|
|
"/apply",
|
|
"/report",
|
|
"/request",
|
|
"/membership/renewals",
|
|
];
|
|
|
|
/** Route prefixes that require admin role */
|
|
const adminOnlyPrefixes = ["/dashboard/admin"];
|
|
|
|
/** Routes that are always public (no auth) */
|
|
const publicPaths = [
|
|
"/",
|
|
"/about",
|
|
"/governance",
|
|
"/leadership",
|
|
"/org-chart",
|
|
"/departments",
|
|
"/programs",
|
|
"/membership",
|
|
"/accreditation",
|
|
"/documents",
|
|
"/standards",
|
|
"/news",
|
|
"/publications",
|
|
"/contact",
|
|
"/regions",
|
|
"/transparency",
|
|
"/login",
|
|
"/logout",
|
|
];
|
|
|
|
function pathIsPublic(pathname: string): boolean {
|
|
if (publicPaths.includes(pathname)) return true;
|
|
if (pathname.startsWith("/transparency/")) return true;
|
|
return false;
|
|
}
|
|
|
|
function pathIsProtected(pathname: string): boolean {
|
|
return protectedPrefixes.some((p) => pathname === p || pathname.startsWith(p + "/"));
|
|
}
|
|
|
|
function pathIsAdminOnly(pathname: string): boolean {
|
|
return adminOnlyPrefixes.some((p) => pathname === p || pathname.startsWith(p + "/"));
|
|
}
|
|
|
|
export function canAccess(pathname: string, role: Role | null): boolean {
|
|
if (pathIsPublic(pathname)) return true;
|
|
if (role == null) return false;
|
|
if (pathIsAdminOnly(pathname)) return role === "admin";
|
|
if (pathIsProtected(pathname)) return true;
|
|
return false;
|
|
}
|
|
|
|
export function requiresAuth(pathname: string): boolean {
|
|
return pathIsProtected(pathname);
|
|
}
|
|
|
|
export function requiresAdmin(pathname: string): boolean {
|
|
return pathIsAdminOnly(pathname);
|
|
}
|