summaryrefslogtreecommitdiff
path: root/garden/src/components/StaffGuard.tsx
blob: 331b69c3db3b8bd5bf930ed0fa0f753f4499af47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { type JSX, Show, createEffect } from "solid-js";
import { useNavigate } from "@solidjs/router";
import { auth } from "../store/auth";
import { UserRole } from "../types/roles";

interface StaffGuardProps {
  children: JSX.Element;
}

function isStaff(role?: string) {
  return role === UserRole.Owner || role === UserRole.Admin || role === UserRole.Moderator;
}

export default function StaffGuard(props: StaffGuardProps) {
  const navigate = useNavigate();

  createEffect(() => {
    if (!auth.loading() && (!auth.user() || !isStaff(auth.user()?.role))) {
      navigate("/", { replace: true });
    }
  });

  return (
    <Show when={!auth.loading() && auth.user() && isStaff(auth.user()?.role)}>
      {props.children}
    </Show>
  );
}