27 lines
804 B
TypeScript
27 lines
804 B
TypeScript
'use client';
|
|
|
|
import { useEffect } from 'react';
|
|
import { usePathname, useRouter } from 'next/navigation';
|
|
import { useAuth } from '@/components/id/auth-provider';
|
|
|
|
export function useRequireAuth() {
|
|
const router = useRouter();
|
|
const pathname = usePathname();
|
|
const { user, isLoading, isApiReady, isPinLocked, hasStoredSession } = useAuth();
|
|
|
|
useEffect(() => {
|
|
if (isLoading || !isApiReady) return;
|
|
if (user || isPinLocked || hasStoredSession) return;
|
|
if (pathname.startsWith('/auth/')) return;
|
|
router.replace('/auth/login');
|
|
}, [hasStoredSession, isApiReady, isLoading, isPinLocked, pathname, router, user]);
|
|
|
|
return {
|
|
user,
|
|
isLoading,
|
|
isPinLocked,
|
|
isApiReady,
|
|
isReady: !isLoading && isApiReady && Boolean(user || isPinLocked || hasStoredSession)
|
|
};
|
|
}
|