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';
|
'use client';
|
||||||
|
|
||||||
import { useAuth } from '@/lib/auth';
|
import { useAuth } from '@/lib/auth';
|
||||||
import { useRouter } from 'next/navigation';
|
import { usePathname, useRouter } from 'next/navigation';
|
||||||
import { useEffect, type ReactNode } from 'react';
|
import { useEffect, type ReactNode } from 'react';
|
||||||
|
|
||||||
export default function AuthGuard({ children }: { children: ReactNode }) {
|
export default function AuthGuard({ children }: { children: ReactNode }) {
|
||||||
const { user, isLoading } = useAuth();
|
const { user, isLoading } = useAuth();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const pathname = usePathname();
|
||||||
|
|
||||||
|
const isLoginPage = pathname === '/login';
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isLoading && !user) router.replace('/login');
|
if (!isLoginPage && !isLoading && !user) router.replace('/login');
|
||||||
}, [user, isLoading, router]);
|
}, [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 (isLoading) return <div className="flex h-screen items-center justify-center text-muted-foreground">Cargando...</div>;
|
||||||
if (!user) return null;
|
if (!user) return null;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user