copied the code from the working repo
This commit is contained in:
30
mtucijobsweb2/fsd/app/provider/AuthGuard.tsx
Normal file
30
mtucijobsweb2/fsd/app/provider/AuthGuard.tsx
Normal file
@@ -0,0 +1,30 @@
|
||||
'use client';
|
||||
import { useRouter, usePathname } from 'next/navigation';
|
||||
import { useEffect } from 'react';
|
||||
import { useAuth } from '@/fsd/app/provider/AuthContext';
|
||||
|
||||
const AuthGuard: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||
const { isAuthenticated, loading } = useAuth();
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
|
||||
useEffect(() => {
|
||||
if (!loading) {
|
||||
if (!isAuthenticated && pathname !== '/login') {
|
||||
router.replace('/login');
|
||||
}
|
||||
}
|
||||
}, [loading, isAuthenticated, pathname, router]);
|
||||
|
||||
if (loading) {
|
||||
return <div>Loading...</div>; // Показываем загрузку
|
||||
}
|
||||
|
||||
if (!isAuthenticated && pathname !== '/login') {
|
||||
return null; // Не рендерим контент до завершения редиректа
|
||||
}
|
||||
|
||||
return <>{children}</>;
|
||||
};
|
||||
|
||||
export default AuthGuard;
|
||||
Reference in New Issue
Block a user