-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpage.tsx
More file actions
56 lines (49 loc) · 1.67 KB
/
page.tsx
File metadata and controls
56 lines (49 loc) · 1.67 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"use client";
import { useUser, SignOutButton } from "@clerk/nextjs";
import { useRouter } from "next/navigation";
import { Flex, Heading, Text, VStack, Box, Button, Spinner } from "@chakra-ui/react";
import { useEffect } from "react";
export default function AdminDashboardPage() {
const { user, isLoaded } = useUser();
const router = useRouter();
// 1. SECURITY CHECK (Client Side)
useEffect(() => {
if (isLoaded) {
if (!user) {
// Not logged in? Go to login
router.push("/login/admin");
} else if (user.publicMetadata?.role !== "admin") {
// Logged in but not Admin? Kick them out
router.push("/");
}
}
}, [isLoaded, user, router]);
// 2. Loading State (Prevents "Flash" of content)
if (!isLoaded) {
return (
<Flex height="100vh" alignItems="center" justifyContent="center">
<Spinner size="xl" color="blue.500" />
</Flex>
);
}
// 3. Final Gate: If checks fail, don't show anything (wait for redirect)
if (!user || user.publicMetadata?.role !== "admin") return null;
// 4. Render Dashboard
return (
<Flex height="100vh" alignItems="center" justifyContent="center">
<VStack gap={4}>
<Heading color="blue.600">Admin Dashboard</Heading>
<Text>Welcome, {user.firstName}! You have full access.</Text>
<Text fontSize="sm" color="gray.500">
Security Status: Verified Admin
</Text>
{/* THIS BUTTON KILLS THE SESSION INSTANTLY */}
<Box>
<SignOutButton redirectUrl="/login/admin">
<Button colorScheme="red">Sign Out (Reset Test)</Button>
</SignOutButton>
</Box>
</VStack>
</Flex>
);
}