fix: allow /login route to bypass AuthGuard so login page renders instead of blank
This commit is contained in:
@@ -1,17 +1,21 @@
|
||||
'use client';
|
||||
|
||||
import { useAuth } from '@/lib/auth';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { usePathname, useRouter } from 'next/navigation';
|
||||
import { useEffect, type ReactNode } from 'react';
|
||||
|
||||
export default function AuthGuard({ children }: { children: ReactNode }) {
|
||||
const { user, isLoading } = useAuth();
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
|
||||
const isLoginPage = pathname === '/login';
|
||||
|
||||
useEffect(() => {
|
||||
if (!isLoading && !user) router.replace('/login');
|
||||
}, [user, isLoading, router]);
|
||||
if (!isLoginPage && !isLoading && !user) router.replace('/login');
|
||||
}, [user, isLoading, router, isLoginPage]);
|
||||
|
||||
if (isLoginPage) return <>{children}</>;
|
||||
if (isLoading) return <div className="flex h-screen items-center justify-center text-muted-foreground">Cargando...</div>;
|
||||
if (!user) return null;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user