30 lines
1.0 KiB
TypeScript
30 lines
1.0 KiB
TypeScript
import type { PropsWithChildren } from "react";
|
|
import { Navigate, useLocation } from "react-router-dom";
|
|
|
|
import { Spinner } from "../components/Spinner";
|
|
import { useMe } from "../hooks/auth";
|
|
import { useAdminMe } from "../hooks/admin";
|
|
|
|
export function RequireAuth({ children }: PropsWithChildren) {
|
|
const { data: me, isLoading } = useMe();
|
|
const location = useLocation();
|
|
if (isLoading) return <Spinner />;
|
|
if (!me) return <Navigate to="/login" replace state={{ from: location }} />;
|
|
return <>{children}</>;
|
|
}
|
|
|
|
export function RequireGroup({ children }: PropsWithChildren) {
|
|
const { data: me, isLoading } = useMe();
|
|
if (isLoading) return <Spinner />;
|
|
if (!me) return <Navigate to="/login" replace />;
|
|
if (me.active_group_id == null) return <Navigate to="/" replace />;
|
|
return <>{children}</>;
|
|
}
|
|
|
|
export function RequireAdmin({ children }: PropsWithChildren) {
|
|
const { data: admin, isLoading } = useAdminMe();
|
|
if (isLoading) return <Spinner />;
|
|
if (!admin) return <Navigate to="/admin/login" replace />;
|
|
return <>{children}</>;
|
|
}
|