|
| 1 | +"use client"; |
| 2 | + |
1 | 3 | import { Icons } from "@/components/icons"; |
2 | 4 | import Link from "next/link"; |
3 | 5 | import Image from "next/image"; |
| 6 | +import { useRouter } from "next/navigation"; |
| 7 | +import { useEffect, useState } from "react"; |
4 | 8 |
|
5 | 9 | export default function AuthLayout({ |
6 | 10 | children, |
7 | 11 | }: { |
8 | 12 | children: React.ReactNode; |
9 | 13 | }) { |
| 14 | + const router = useRouter(); |
| 15 | + const [isChecking, setIsChecking] = useState(true); |
| 16 | + |
| 17 | + // Redirect if already logged in |
| 18 | + useEffect(() => { |
| 19 | + async function checkAuth() { |
| 20 | + try { |
| 21 | + const response = await fetch('/api/users/me', { |
| 22 | + credentials: 'include', |
| 23 | + }); |
| 24 | + |
| 25 | + if (response.ok) { |
| 26 | + // User is already authenticated, redirect to projects |
| 27 | + router.push('/projects'); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + setIsChecking(false); |
| 32 | + } catch { |
| 33 | + // User is not authenticated, stay on auth page |
| 34 | + setIsChecking(false); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + checkAuth(); |
| 39 | + }, [router]); |
| 40 | + |
| 41 | + // Show loading state while checking authentication |
| 42 | + if (isChecking) { |
| 43 | + return ( |
| 44 | + <div className="flex h-screen items-center justify-center"> |
| 45 | + <div className="flex flex-col items-center gap-2"> |
| 46 | + <div className="h-8 w-8 animate-spin rounded-full border-4 border-primary border-t-transparent" /> |
| 47 | + <p className="text-sm text-muted-foreground">Verifying authentication...</p> |
| 48 | + </div> |
| 49 | + </div> |
| 50 | + ); |
| 51 | + } |
| 52 | + |
10 | 53 | return ( |
11 | 54 | <div className="min-h-screen grid lg:grid-cols-2"> |
12 | 55 | {/* Left side - Branding */} |
|
0 commit comments