copied the code from the working repo
This commit is contained in:
25
mtucijobsweb2/fsd/app/provider/withAuth.tsx
Normal file
25
mtucijobsweb2/fsd/app/provider/withAuth.tsx
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
'use client';
|
||||
|
||||
import React, { useEffect } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useAuth } from '@/fsd/app/provider/AuthContext';
|
||||
|
||||
const AuthGuard: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||
const { isAuthenticated } = useAuth();
|
||||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
if (!isAuthenticated) {
|
||||
router.replace('/login');
|
||||
}
|
||||
}, [isAuthenticated, router]);
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return null; // Или можно отобразить загрузочный экран
|
||||
}
|
||||
|
||||
return <>{children}</>;
|
||||
};
|
||||
|
||||
export default AuthGuard;
|
||||
Reference in New Issue
Block a user