hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { Link, Navigate } from "@tanstack/solid-router"; |
| 2 | import { type ParentProps, Show } from "solid-js"; |
| 3 | import type { Role } from "@/api/client"; |
| 4 | import { formatApiError } from "@/api/client"; |
| 5 | import { Alert } from "@/components/ui/alert"; |
| 6 | import { Button } from "@/components/ui/button"; |
| 7 | import { IconHome, IconLock } from "@/components/ui/icons"; |
| 8 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 9 | import { hasExactRole, roleInRange } from "@/lib/roles"; |
| 10 | import { useAuth } from "@/stores/auth-context"; |
| 11 | import { useT } from "@/stores/preferences-context"; |
| 12 | |
| 13 | function 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 | |
| 38 | export 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 | } |