|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useUser, SignOutButton } from "@clerk/nextjs"; |
| 4 | +import { useRouter } from "next/navigation"; |
| 5 | +import { Flex, Heading, Text, VStack, Box, Button, Spinner } from "@chakra-ui/react"; |
| 6 | +import { useEffect } from "react"; |
| 7 | + |
| 8 | +export default function AdminDashboardPage() { |
| 9 | + const { user, isLoaded } = useUser(); |
| 10 | + const router = useRouter(); |
| 11 | + |
| 12 | + // 1. SECURITY CHECK (Client Side) |
| 13 | + useEffect(() => { |
| 14 | + if (isLoaded) { |
| 15 | + if (!user) { |
| 16 | + // Not logged in? Go to login |
| 17 | + router.push("/login/admin"); |
| 18 | + } else if (user.publicMetadata?.role !== "admin") { |
| 19 | + // Logged in but not Admin? Kick them out |
| 20 | + router.push("/"); |
| 21 | + } |
| 22 | + } |
| 23 | + }, [isLoaded, user, router]); |
| 24 | + |
| 25 | + // 2. Loading State (Prevents "Flash" of content) |
| 26 | + if (!isLoaded) { |
| 27 | + return ( |
| 28 | + <Flex height="100vh" alignItems="center" justifyContent="center"> |
| 29 | + <Spinner size="xl" color="blue.500" /> |
| 30 | + </Flex> |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + // 3. Final Gate: If checks fail, don't show anything (wait for redirect) |
| 35 | + if (!user || user.publicMetadata?.role !== "admin") return null; |
| 36 | + |
| 37 | + // 4. Render Dashboard |
| 38 | + return ( |
| 39 | + <Flex height="100vh" alignItems="center" justifyContent="center"> |
| 40 | + <VStack gap={4}> |
| 41 | + <Heading color="blue.600">Admin Dashboard</Heading> |
| 42 | + <Text>Welcome, {user.firstName}! You have full access.</Text> |
| 43 | + <Text fontSize="sm" color="gray.500"> |
| 44 | + Security Status: Verified Admin |
| 45 | + </Text> |
| 46 | + |
| 47 | + {/* THIS BUTTON KILLS THE SESSION INSTANTLY */} |
| 48 | + <Box> |
| 49 | + <SignOutButton redirectUrl="/login/admin"> |
| 50 | + <Button colorScheme="red">Sign Out (Reset Test)</Button> |
| 51 | + </SignOutButton> |
| 52 | + </Box> |
| 53 | + </VStack> |
| 54 | + </Flex> |
| 55 | + ); |
| 56 | +} |
0 commit comments