CoolFace
Apppublic

Leon4gr45/builder

sourceHugging Facemitupdated 2d agoView on Hugging Face
0likes
use-auth.ts44 linesDownload Raw Back to hooks
1'use client';2 3import { useEffect, useState } from 'react';4import { useRouter } from 'next/navigation';5 6export function useAuth() {7  const router = useRouter();8  const [isChecking, setIsChecking] = useState(true);9 10  useEffect(() => {11    const checkAuth = async () => {12      // Only check auth in server mode13      if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') {14        setIsChecking(false);15        return;16      }17 18      try {19        const response = await fetch('/api/auth/check');20 21        if (response.status === 401) {22          // Not authenticated, redirect to login23          router.push('/admin/login');24          return;25        }26 27        if (!response.ok) {28          throw new Error('Auth check failed');29        }30 31        // Authenticated32        setIsChecking(false);33      } catch (error) {34        console.error('Auth check error:', error);35        router.push('/admin/login');36      }37    };38 39    checkAuth();40  }, [router]);41 42  return { isChecking };43}44