85b31e9528
Co-authored-by: ISMAIL MASSERAN <topaz@Mac.dlinkrouter.local> Co-authored-by: ISMAIL MASSERAN <topaz@ISMAILs-Macbook.local> Reviewed-on: #1
30 lines
648 B
React
30 lines
648 B
React
import { useEffect } from 'react';
|
|
import { useNavigate } from 'react-router-dom';
|
|
import { getUserData } from '../../utils/session.js';
|
|
|
|
export default function AuthGuard({ children, roles }) {
|
|
const navigate = useNavigate();
|
|
|
|
useEffect(() => {
|
|
const user = getUserData();
|
|
|
|
if (!user) {
|
|
navigate('/login');
|
|
return;
|
|
}
|
|
|
|
const hasNecessaryRole = roles.some(role => user.roles.find(userRole => userRole === role));
|
|
|
|
if (!hasNecessaryRole) {
|
|
navigate('/');
|
|
return;
|
|
}
|
|
}, []);
|
|
|
|
return (
|
|
<>
|
|
{children}
|
|
</>
|
|
);
|
|
|
|
} |