aethex-forge/client/components/RequireAccess.tsx
2025-10-19 05:11:41 +00:00

85 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Navigate, useLocation } from "react-router-dom";
import { useAuth } from "@/contexts/AuthContext";
import Layout from "@/components/Layout";
import {
Card,
CardHeader,
CardTitle,
CardDescription,
} from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
interface RequireAccessProps {
allowedRealms?: Array<
"game_developer" | "client" | "community_member" | "customer" | "staff"
>;
allowedRoles?: string[];
children: React.ReactElement;
}
export default function RequireAccess({
allowedRealms,
allowedRoles,
children,
}: RequireAccessProps) {
const { user, profile, roles } = useAuth();
const realmOk =
!allowedRealms || allowedRealms.includes((profile as any)?.user_type);
const rolesOk =
!allowedRoles ||
(Array.isArray(roles) &&
roles.some((r) => allowedRoles.includes(r.toLowerCase())));
if (!user) {
return (
<Layout>
<div className="min-h-screen bg-aethex-gradient py-12">
<section className="container mx-auto max-w-3xl px-4">
<Card className="bg-card/60 border-border/40 backdrop-blur">
<CardHeader>
<Badge
variant="outline"
className="mb-2 border-aethex-400/50 text-aethex-300"
>
Access
</Badge>
<CardTitle>Access denied</CardTitle>
<CardDescription>
Sign in required to view this page.
</CardDescription>
</CardHeader>
</Card>
</section>
</div>
</Layout>
);
}
if (!realmOk || !rolesOk) {
return (
<Layout>
<div className="min-h-screen bg-aethex-gradient py-12">
<section className="container mx-auto max-w-3xl px-4">
<Card className="bg-card/60 border-border/40 backdrop-blur">
<CardHeader>
<Badge
variant="outline"
className="mb-2 border-aethex-400/50 text-aethex-300"
>
Access
</Badge>
<CardTitle>Access denied</CardTitle>
<CardDescription>
You dont have the required realm or role for this area.
</CardDescription>
</CardHeader>
</Card>
</section>
</div>
</Layout>
);
}
return children;
}