mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-28 23:43:22 +00:00
feat: Création d'un profile selector [#37,#38]
This commit is contained in:
@ -1,33 +1,33 @@
|
||||
import React, { useEffect } from 'react';
|
||||
import { useEffect } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useSession } from 'next-auth/react';
|
||||
import Loader from '@/components/Loader'; // Importez le composant Loader
|
||||
import { FE_USERS_LOGIN_URL } from '@/utils/Url';
|
||||
import { useEstablishment } from '@/context/EstablishmentContext';
|
||||
import { FE_USERS_LOGIN_URL,getRedirectUrlFromRole } from '@/utils/Url';
|
||||
|
||||
const ProtectedRoute = ({ children, requiredRight }) => {
|
||||
const { data: session, status } = useSession({
|
||||
required: true,
|
||||
onUnauthenticated() {
|
||||
router.push(`${FE_USERS_LOGIN_URL}`);
|
||||
}
|
||||
});
|
||||
const router = useRouter();
|
||||
|
||||
// Ne vérifier que si le statut est définitif
|
||||
if (status === 'loading') {
|
||||
return <Loader />;
|
||||
}
|
||||
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
|
||||
const hasRequiredRight = session?.user?.roles?.some(role => role.role_type === 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]);
|
||||
|
||||
|
||||
if (session && requiredRight && !hasRequiredRight) {
|
||||
router.push(`${FE_USERS_LOGIN_URL}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Autoriser l'affichage si authentifié et rôle correct
|
||||
return session ? children : null;
|
||||
return hasRequiredRight ? children : null;
|
||||
};
|
||||
|
||||
export default ProtectedRoute;
|
||||
Reference in New Issue
Block a user