mirror of
https://github.com/johndoe6345789/docker-swarm-termina.git
synced 2026-05-04 10:34:58 +00:00
87daa3add3
- Extract login form logic into useLoginForm hook - Create useAuthRedirect hook for auth-based navigation - Refactor LoginForm component to use useLoginForm (147 -> 135 LOC) - Refactor login page to use useAuthRedirect (23 -> 14 LOC) - Update dashboard to use useAuthRedirect for cleaner code - Improve code reusability and testability https://claude.ai/code/session_01U3wVqokhrL3dTeq2dTq73n
21 lines
628 B
TypeScript
21 lines
628 B
TypeScript
import { useEffect } from 'react';
|
|
import { useRouter } from 'next/navigation';
|
|
import { useAppSelector } from '@/lib/store/hooks';
|
|
|
|
export function useAuthRedirect(redirectTo: '/dashboard' | '/') {
|
|
const { isAuthenticated, loading } = useAppSelector((state) => state.auth);
|
|
const router = useRouter();
|
|
|
|
useEffect(() => {
|
|
if (loading) return;
|
|
|
|
if (redirectTo === '/dashboard' && isAuthenticated) {
|
|
router.push('/dashboard');
|
|
} else if (redirectTo === '/' && !isAuthenticated) {
|
|
router.push('/');
|
|
}
|
|
}, [isAuthenticated, loading, router, redirectTo]);
|
|
|
|
return { isAuthenticated, loading };
|
|
}
|