protected-layout.tsx76 lines, 2.1 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { useCurrentUser } from "@/hooks/use-current-user"; |
| 4 | import { Loader2, LockIcon } from "lucide-react"; |
| 5 | import { |
| 6 | Card, |
| 7 | CardContent, |
| 8 | CardDescription, |
| 9 | CardHeader, |
| 10 | CardTitle, |
| 11 | } from "../ui/card"; |
| 12 | import { Button } from "../ui/button"; |
| 13 | import Link from "next/link"; |
| 14 | import { useModalStore } from "@/store/zustand"; |
| 15 | |
| 16 | export function ProtectedLayout({ children }: { children: React.ReactNode }) { |
| 17 | const { user, isLoading } = useCurrentUser(); |
| 18 | |
| 19 | if (isLoading) { |
| 20 | return ( |
| 21 | <div className="flex items-center justify-center h-screen"> |
| 22 | <div className="flex items-center justify-center"> |
| 23 | <Loader2 className="size-4 mr-2 animate-spin" /> |
| 24 | </div> |
| 25 | </div> |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | if (!user) { |
| 30 | return ( |
| 31 | <div className="flex items-center justify-center h-screen"> |
| 32 | <AuthCard /> |
| 33 | </div> |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | return <>{children}</>; |
| 38 | } |
| 39 | |
| 40 | export default function AuthCard() { |
| 41 | const { openModal } = useModalStore(); |
| 42 | |
| 43 | return ( |
| 44 | <Card className="w-full max-w-3xl mx-auto"> |
| 45 | <div className="flex flex-col sm:flex-row"> |
| 46 | <div className="sm:w-1/4 bg-primary/10 flex items-center justify-center p-4"> |
| 47 | <LockIcon className="size-16 text-primary" /> |
| 48 | </div> |
| 49 | <div className="sm:w-3/4 p-4"> |
| 50 | <CardHeader className="space-y-1 px-0 pb-2"> |
| 51 | <CardTitle className="text-2xl font-bold"> |
| 52 | Authentication required |
| 53 | </CardTitle> |
| 54 | <CardDescription> |
| 55 | You need to be logged in to access this page. |
| 56 | </CardDescription> |
| 57 | </CardHeader> |
| 58 | <CardContent className="px-0 py-2"> |
| 59 | <div className="flex flex-col sm:flex-row gap-2"> |
| 60 | <Button |
| 61 | onClick={() => openModal("connectAccountModal")} |
| 62 | className="flex-1" |
| 63 | variant={"outline"} |
| 64 | > |
| 65 | Continue with Google |
| 66 | </Button> |
| 67 | <Link href={"/"} className="flex-1"> |
| 68 | <Button className="w-full">Back to Home</Button> |
| 69 | </Link> |
| 70 | </div> |
| 71 | </CardContent> |
| 72 | </div> |
| 73 | </div> |
| 74 | </Card> |
| 75 | ); |
| 76 | } |