From e1e4e81420ca9c6dad72372588a7da945f2c297a Mon Sep 17 00:00:00 2001 From: Ouijdane IMER Date: Tue, 12 Aug 2025 16:14:44 +0200 Subject: [PATCH] connexion avec o365 --- project/package-lock.json | 36 +++++++++++++++++++++ project/package.json | 2 ++ project/src/AuthConfig.js | 12 +++++++ project/src/context/AuthContext.jsx | 49 ++++++++++++++++++++++++++++- project/src/pages/Login.jsx | 27 ++++++++++++---- 5 files changed, 119 insertions(+), 7 deletions(-) create mode 100644 project/src/AuthConfig.js diff --git a/project/package-lock.json b/project/package-lock.json index 5cb466d..c3df0ea 100644 --- a/project/package-lock.json +++ b/project/package-lock.json @@ -8,6 +8,8 @@ "name": "gestion-conges-jsx", "version": "0.0.0", "dependencies": { + "@azure/msal-browser": "^4.19.0", + "@azure/msal-react": "^3.0.17", "lucide-react": "^0.344.0", "react": "^18.3.1", "react-dom": "^18.3.1", @@ -46,6 +48,40 @@ "node": ">=6.0.0" } }, + "node_modules/@azure/msal-browser": { + "version": "4.19.0", + "resolved": "https://registry.npmjs.org/@azure/msal-browser/-/msal-browser-4.19.0.tgz", + "integrity": "sha512-g6Ea+sJmK7l5NUyrPhtD7DNj/tZcsr6VTNNLNuYs8yPvL3HNiIpO/0kzXntF9AqJ/6L+uz9aHmoT1x+RNq6zBQ==", + "license": "MIT", + "dependencies": { + "@azure/msal-common": "15.10.0" + }, + "engines": { + "node": ">=0.8.0" + } + }, + "node_modules/@azure/msal-common": { + "version": "15.10.0", + "resolved": "https://registry.npmjs.org/@azure/msal-common/-/msal-common-15.10.0.tgz", + "integrity": "sha512-+cGnma71NV3jzl6DdgdHsqriN4ZA7puBIzObSYCvcIVGMULGb2NrcOGV6IJxO06HoVRHFKijkxd9lcBvS063KQ==", + "license": "MIT", + "engines": { + "node": ">=0.8.0" + } + }, + "node_modules/@azure/msal-react": { + "version": "3.0.17", + "resolved": "https://registry.npmjs.org/@azure/msal-react/-/msal-react-3.0.17.tgz", + "integrity": "sha512-GgVn8OQmtXMPJ88+w8E+7hpWXcVWhh8aIjspkrJr4bbONWhbfyQOSyN92gsrSbynIsJ9o7GWTjvGHFLr2MuyQQ==", + "license": "MIT", + "engines": { + "node": ">=10" + }, + "peerDependencies": { + "@azure/msal-browser": "^4.19.0", + "react": "^16.8.0 || ^17 || ^18 || ^19" + } + }, "node_modules/@babel/code-frame": { "version": "7.25.7", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.25.7.tgz", diff --git a/project/package.json b/project/package.json index f6f0f2a..32c8abb 100644 --- a/project/package.json +++ b/project/package.json @@ -9,6 +9,8 @@ "preview": "vite preview" }, "dependencies": { + "@azure/msal-browser": "^4.19.0", + "@azure/msal-react": "^3.0.17", "lucide-react": "^0.344.0", "react": "^18.3.1", "react-dom": "^18.3.1", diff --git a/project/src/AuthConfig.js b/project/src/AuthConfig.js new file mode 100644 index 0000000..21c7b82 --- /dev/null +++ b/project/src/AuthConfig.js @@ -0,0 +1,12 @@ +// authConfig.js +export const msalConfig = { + auth: { + clientId: "4bb4cc24-bac3-427c-b02c-5d14fc67b561", // Application (client) ID dans Azure + authority: "https://login.microsoftonline.com/9840a2a0-6ae1-4688-b03d-d2ec291be0f9", // Directory (tenant) ID + redirectUri: "http://localhost:5173" + } +}; + +export const loginRequest = { + scopes: ["User.Read"] // Permet de lire le profil utilisateur +}; diff --git a/project/src/context/AuthContext.jsx b/project/src/context/AuthContext.jsx index 0d1f8a4..81d8f55 100644 --- a/project/src/context/AuthContext.jsx +++ b/project/src/context/AuthContext.jsx @@ -1,4 +1,5 @@ import React, { createContext, useContext, useState, useEffect } from 'react'; +import * as msal from '@azure/msal-browser'; const AuthContext = createContext(); @@ -10,11 +11,32 @@ export const useAuth = () => { return context; }; +const msalConfig = { + auth: { + clientId: '4bb4cc24-bac3-427c-b02c-5d14fc67b561', + authority: 'https://login.microsoftonline.com/9840a2a0-6ae1-4688-b03d-d2ec291be0f9', + redirectUri: window.location.origin, + }, +}; + +const msalInstance = new msal.PublicClientApplication(msalConfig); + export const AuthProvider = ({ children }) => { const [user, setUser] = useState(null); const [isLoading, setIsLoading] = useState(true); + // Initialise MSAL au montage useEffect(() => { + const initializeMsal = async () => { + try { + await msalInstance.initialize(); + } catch (error) { + console.error("Erreur d'initialisation MSAL:", error); + } + }; + + initializeMsal(); + const savedUser = localStorage.getItem('user'); if (savedUser) { try { @@ -88,12 +110,37 @@ export const AuthProvider = ({ children }) => { } }; + const loginWithO365 = async () => { + try { + const loginResponse = await msalInstance.loginPopup({ + scopes: ["user.read"] + }); + const account = loginResponse.account; + if (account) { + const userData = { + id: account.homeAccountId, + name: account.name, + email: account.username, + role: 'Employe', + service: 'Non défini', + }; + setUser(userData); + localStorage.setItem('user', JSON.stringify(userData)); + return true; + } + return false; + } catch (error) { + console.error('Erreur login Office 365:', error); + return false; + } + }; + const logout = () => { setUser(null); localStorage.removeItem('user'); }; - const value = { user, login, logout, isLoading }; + const value = { user, login, loginWithO365, logout, isLoading }; return ( diff --git a/project/src/pages/Login.jsx b/project/src/pages/Login.jsx index 50d8f70..1803ad3 100644 --- a/project/src/pages/Login.jsx +++ b/project/src/pages/Login.jsx @@ -9,8 +9,9 @@ const Login = () => { const [showPassword, setShowPassword] = useState(false); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(''); - const { login } = useAuth(); + const navigate = useNavigate(); + const { login, loginWithO365 } = useAuth(); const handleSubmit = async (e) => { e.preventDefault(); @@ -33,8 +34,6 @@ const Login = () => {
- -
@@ -92,12 +91,28 @@ const Login = () => { className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-500 hover:text-gray-900" title={showPassword ? "Masquer le mot de passe" : "Afficher le mot de passe"} > - {/* L'icône est choisie en fonction de l'état de showPassword */} - + {showPassword ? : } +
+ +
+ {error && (

{error}

@@ -119,4 +134,4 @@ const Login = () => { ); }; -export default Login; \ No newline at end of file +export default Login;