mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-28 23:43:22 +00:00
33 lines
1.0 KiB
JavaScript
33 lines
1.0 KiB
JavaScript
import { useEffect } from 'react';
|
|
import { useRouter } from 'next/navigation';
|
|
import { useEstablishment } from '@/context/EstablishmentContext';
|
|
import { FE_USERS_LOGIN_URL,getRedirectUrlFromRole } from '@/utils/Url';
|
|
|
|
const ProtectedRoute = ({ children, requiredRight }) => {
|
|
|
|
const { user, profileRole } = useEstablishment();
|
|
const router = useRouter();
|
|
const hasRequiredRight = (profileRole === requiredRight);
|
|
|
|
// Vérifier si l'utilisateur a au moins un rôle correspondant au requiredRight
|
|
useEffect(() => {
|
|
if(user){
|
|
// Vérifier si l'utilisateur a le droit requis mais pas le bon role on le redirige la page d'accueil associé au role
|
|
if (!hasRequiredRight) {
|
|
const redirectUrl = getRedirectUrlFromRole(profileRole);
|
|
router.push(`${redirectUrl}`);
|
|
}
|
|
|
|
}else{
|
|
// User non authentifié
|
|
router.push(`${FE_USERS_LOGIN_URL}`);
|
|
}
|
|
}, [profileRole]);
|
|
|
|
|
|
|
|
// Autoriser l'affichage si authentifié et rôle correct
|
|
return hasRequiredRight ? children : null;
|
|
};
|
|
|
|
export default ProtectedRoute; |