mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-29 16:03:21 +00:00
refactor: Creation d'un provider et d'un systeme de middleware
This commit is contained in:
@ -26,10 +26,11 @@ import {
|
||||
FE_ADMIN_SETTINGS_URL
|
||||
} from '@/utils/Url';
|
||||
|
||||
import { disconnect } from '@/app/actions/authAction';
|
||||
import { disconnect, getUser } from '@/app/actions/authAction';
|
||||
import { useSession } from 'next-auth/react';
|
||||
import { fetchEstablishment } from '@/app/actions/schoolAction';
|
||||
import ProtectedRoute from '@/components/ProtectedRoute';
|
||||
import { SessionProvider } from 'next-auth/react';
|
||||
import { getGravatarUrl } from '@/utils/gravatar';
|
||||
|
||||
export default function Layout({
|
||||
children,
|
||||
@ -48,6 +49,8 @@ export default function Layout({
|
||||
const [establishment, setEstablishment] = useState(null);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [isPopupVisible, setIsPopupVisible] = useState(false);
|
||||
const [user, setUser] = useState(null);
|
||||
const { data: session } = useSession();
|
||||
|
||||
const pathname = usePathname();
|
||||
const currentPage = pathname.split('/').pop();
|
||||
@ -84,8 +87,18 @@ export default function Layout({
|
||||
.finally(() => setIsLoading(false));
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchUser = async () => {
|
||||
if (session) { // Vérifier que la session existe
|
||||
const userData = await getUser();
|
||||
setUser(userData);
|
||||
}
|
||||
};
|
||||
|
||||
fetchUser();
|
||||
}, [session]);
|
||||
|
||||
return (
|
||||
<SessionProvider>
|
||||
<ProtectedRoute>
|
||||
{!isLoading && (
|
||||
<div className="flex min-h-screen bg-gray-50">
|
||||
@ -95,7 +108,15 @@ export default function Layout({
|
||||
<header className="h-16 bg-white border-b border-gray-200 px-8 py-4 flex items-center justify-between z-9">
|
||||
<div className="text-xl font-semibold">{headerTitle}</div>
|
||||
<DropdownMenu
|
||||
buttonContent={<Image src="https://i.pravatar.cc/32" alt="Profile" className="w-8 h-8 rounded-full cursor-pointer" width={150} height={150} />}
|
||||
buttonContent={
|
||||
<Image
|
||||
src={getGravatarUrl(user?.email)}
|
||||
alt="Profile"
|
||||
className="w-8 h-8 rounded-full cursor-pointer"
|
||||
width={32}
|
||||
height={32}
|
||||
/>
|
||||
}
|
||||
items={dropdownItems}
|
||||
buttonClassName=""
|
||||
menuClassName="absolute right-0 mt-2 w-48 bg-white border border-gray-200 rounded shadow-lg"
|
||||
@ -126,7 +147,6 @@ export default function Layout({
|
||||
onCancel={() => setIsPopupVisible(false)}
|
||||
/>
|
||||
</ProtectedRoute>
|
||||
</SessionProvider>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -9,7 +9,6 @@ import { FE_PARENTS_HOME_URL,FE_PARENTS_MESSAGERIE_URL,FE_PARENTS_SETTINGS_URL
|
||||
import useLocalStorage from '@/hooks/useLocalStorage';
|
||||
import { fetchMessages } from '@/app/actions/messagerieAction';
|
||||
import ProtectedRoute from '@/components/ProtectedRoute';
|
||||
import { SessionProvider } from 'next-auth/react';
|
||||
import { disconnect } from '@/app/actions/authAction';
|
||||
import Popup from '@/components/Popup';
|
||||
|
||||
@ -55,7 +54,6 @@ export default function Layout({
|
||||
}
|
||||
|
||||
return (
|
||||
<SessionProvider>
|
||||
<ProtectedRoute>
|
||||
<div className="flex flex-col min-h-screen bg-gray-50">
|
||||
{/* Entête */}
|
||||
@ -111,7 +109,6 @@ export default function Layout({
|
||||
onCancel={() => setIsPopupVisible(false)}
|
||||
/>
|
||||
</ProtectedRoute>
|
||||
</SessionProvider>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -2,11 +2,12 @@
|
||||
import React, { useState, useRef, useEffect } from 'react';
|
||||
import { SendHorizontal } from 'lucide-react';
|
||||
import Image from 'next/image';
|
||||
import { getGravatarUrl } from '@/utils/gravatar';
|
||||
|
||||
const contacts = [
|
||||
{ id: 1, name: 'Facturation', profilePic: 'https://i.pravatar.cc/32' },
|
||||
{ id: 2, name: 'Enseignant 1', profilePic: 'https://i.pravatar.cc/32' },
|
||||
{ id: 3, name: 'Contact', profilePic: 'https://i.pravatar.cc/32' },
|
||||
{ id: 1, name: 'Facturation', profilePic: getGravatarUrl('facturation@n3wtschool.com') },
|
||||
{ id: 2, name: 'Enseignant 1', profilePic: getGravatarUrl('enseignant@n3wtschool.com') },
|
||||
{ id: 3, name: 'Contact', profilePic: getGravatarUrl('contact@n3wtschool.com') },
|
||||
];
|
||||
|
||||
export default function MessageriePage() {
|
||||
|
||||
@ -160,4 +160,28 @@ export const getResetPassword = (uuid) => {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}).then(requestResponseHandler);
|
||||
};
|
||||
|
||||
/**
|
||||
* Récupère les informations de l'utilisateur connecté depuis la session
|
||||
* @returns {Promise} Les données de l'utilisateur
|
||||
*/
|
||||
export const getUser = async () => {
|
||||
try {
|
||||
const session = await getSession();
|
||||
|
||||
if (!session || !session.user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
id: session.user.user_id,
|
||||
email: session.user.email,
|
||||
role: session.user.droit
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error getting user from session:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
@ -1,8 +1,8 @@
|
||||
import React from 'react';
|
||||
import { getMessages } from 'next-intl/server';
|
||||
import { NextIntlClientProvider } from 'next-intl';
|
||||
import { CsrfProvider } from '@/context/CsrfContext';
|
||||
import Providers from '@/components/Providers'
|
||||
import "@/css/tailwind.css";
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
export const metadata = {
|
||||
title: "N3WT-SCHOOL",
|
||||
@ -22,17 +22,16 @@ export const metadata = {
|
||||
};
|
||||
|
||||
export default async function RootLayout({ children, params }) {
|
||||
const { locale } = params;
|
||||
const messages = await getMessages(locale); // Passez le locale ici
|
||||
const headersList = headers();
|
||||
const locale = headersList.get('x-locale') || 'fr';
|
||||
const messages = await getMessages(locale);
|
||||
|
||||
return (
|
||||
<html lang={locale}>
|
||||
<body>
|
||||
<CsrfProvider>
|
||||
<NextIntlClientProvider messages={messages} locale={locale}> {/* Passez le locale ici */}
|
||||
{children}
|
||||
</NextIntlClientProvider>
|
||||
</CsrfProvider>
|
||||
<Providers messages={messages} locale={locale} session={params.session}>
|
||||
{children}
|
||||
</Providers>
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user