first
This commit is contained in:
34
src/utils/route-guard/AuthGuard.js
Normal file
34
src/utils/route-guard/AuthGuard.js
Normal file
@@ -0,0 +1,34 @@
|
||||
import PropTypes from 'prop-types';
|
||||
import { useEffect } from 'react';
|
||||
import { useLocation, useNavigate } from 'react-router-dom';
|
||||
|
||||
// project import
|
||||
import useAuth from 'hooks/useAuth';
|
||||
|
||||
// ==============================|| AUTH GUARD ||============================== //
|
||||
|
||||
const AuthGuard = ({ children }) => {
|
||||
const { isLoggedIn } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
const location = useLocation();
|
||||
|
||||
useEffect(() => {
|
||||
if (!isLoggedIn) {
|
||||
navigate('login', {
|
||||
state: {
|
||||
from: location.pathname
|
||||
},
|
||||
replace: true
|
||||
});
|
||||
navigate('login', { replace: true });
|
||||
}
|
||||
}, [isLoggedIn, navigate, location]);
|
||||
|
||||
return children;
|
||||
};
|
||||
|
||||
AuthGuard.propTypes = {
|
||||
children: PropTypes.node
|
||||
};
|
||||
|
||||
export default AuthGuard;
|
||||
Reference in New Issue
Block a user