forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

route-guard.tsx78 lines, 2.9 KiBHistoryRawDownload
1import { Link, Navigate } from "@tanstack/solid-router";
2import { type ParentProps, Show } from "solid-js";
3import type { Role } from "@/api/client";
4import { formatApiError } from "@/api/client";
5import { Alert } from "@/components/ui/alert";
6import { Button } from "@/components/ui/button";
7import { IconHome, IconLock } from "@/components/ui/icons";
8import { PageSpinner } from "@/components/ui/page-spinner";
9import { hasExactRole, roleInRange } from "@/lib/roles";
10import { useAuth } from "@/stores/auth-context";
11import { useT } from "@/stores/preferences-context";
12
13function AccessDeniedCard() {
14 const t = useT();
15 return (
16 <div class="flex min-h-[50vh] items-center justify-center px-4 py-16">
17 <div class="w-full max-w-[420px] space-y-5 rounded-xl border border-border-line bg-surface-base p-8 text-center shadow-[0_10px_24px_-4px_rgba(0,0,0,0.10)]">
18 <div class="mx-auto flex h-14 w-14 items-center justify-center rounded-full bg-warning/10 text-warning-text">
19 <IconLock class="h-6 w-6" />
20 </div>
21 <div class="space-y-2">
22 <p class="text-4xl font-semibold tracking-tight text-text-strong">403</p>
23 <p class="text-base font-semibold text-text-strong">{t("common.accessDenied")}</p>
24 <p class="text-sm leading-[21px] text-text-subtle">{t("errors.accessDeniedDescription")}</p>
25 </div>
26 <Link
27 to="/"
28 class="inline-flex h-9 items-center justify-center gap-1.5 whitespace-nowrap rounded-lg bg-primary px-3 py-2 text-sm font-medium text-primary-foreground transition-colors hover:bg-primary/90"
29 >
30 <IconHome class="mr-1 h-4 w-4" />
31 {t("common.goHome")}
32 </Link>
33 </div>
34 </div>
35 );
36}
37
38export function RouteGuard(
39 props: ParentProps<{
40 minRole?: Role;
41 /** Inclusive upper bound (e.g. maxRole: "manager" excludes admin). */
42 maxRole?: Role;
43 /** When set, only this exact role may enter (e.g. student-only pages). */
44 exactRole?: Role;
45 }>,
46) {
47 const auth = useAuth();
48 const t = useT();
49
50 const allowed = (role: Role) => {
51 if (props.exactRole) return hasExactRole(role, props.exactRole);
52 if (props.minRole || props.maxRole) return roleInRange(role, props.minRole, props.maxRole);
53 return true;
54 };
55
56 return (
57 <Show when={!auth.loading()} fallback={<PageSpinner />}>
58 <Show when={!auth.error()} fallback={
59 <Alert variant="destructive">
60 <div class="space-y-3">
61 <p>{formatApiError(auth.error())}</p>
62 <Button variant="outline" size="sm" onClick={() => void auth.refresh()}>
63 {t("common.tryAgain")}
64 </Button>
65 </div>
66 </Alert>
67 }>
68 <Show when={auth.user()} fallback={<Navigate to="/login" />}>
69 {(u) => (
70 <Show when={allowed(u().role)} fallback={<AccessDeniedCard />}>
71 {props.children}
72 </Show>
73 )}
74 </Show>
75 </Show>
76 </Show>
77 );
78}