|
1 |
| -import { AuthenticationResult, EventType, PublicClientApplication } from "@azure/msal-browser"; |
2 |
| -import { checkLoggedIn, msalConfig, useLogin } from "./authConfig"; |
3 |
| -import { useEffect, useMemo, useRef, useState } from "react"; |
4 |
| -import { MsalProvider } from "@azure/msal-react"; |
| 1 | +import { useEffect, useRef, useState } from "react"; |
| 2 | +import { useMsal } from "@azure/msal-react"; |
| 3 | +import { useLogin, checkLoggedIn } from "./authConfig"; |
5 | 4 | import { LoginContext } from "./loginContext";
|
6 | 5 | import Layout from "./pages/layout/Layout";
|
7 | 6 |
|
8 | 7 | const LayoutWrapper = () => {
|
9 | 8 | const [loggedIn, setLoggedIn] = useState(false);
|
10 | 9 | if (useLogin) {
|
11 |
| - // Create a stable MSAL instance (avoid re-init/duplicate listeners; single shared client for MsalProvider). |
12 |
| - const msalInstance = useMemo(() => new PublicClientApplication(msalConfig), []); |
13 |
| - const [initialized, setInitialized] = useState(false); |
14 |
| - // Track mount state so we don't call setState after unmount if async init resolves late |
| 10 | + const { instance } = useMsal(); |
| 11 | + // Keep track of the mounted state to avoid setting state in an unmounted component |
15 | 12 | const mounted = useRef<boolean>(true);
|
16 |
| - |
17 | 13 | useEffect(() => {
|
18 |
| - // React StrictMode in development invokes effects twice (mount -> cleanup -> mount). |
19 |
| - // Reset the flag here so this run is considered mounted. |
20 | 14 | mounted.current = true;
|
21 |
| - const init = async () => { |
22 |
| - try { |
23 |
| - await msalInstance.initialize(); |
24 |
| - |
25 |
| - // Default to using the first account if no account is active on page load |
26 |
| - if (!msalInstance.getActiveAccount() && msalInstance.getAllAccounts().length > 0) { |
27 |
| - // Account selection logic is app dependent. Adjust as needed for different use cases. |
28 |
| - msalInstance.setActiveAccount(msalInstance.getAllAccounts()[0]); |
29 |
| - } |
30 |
| - |
31 |
| - // Listen for sign-in event and set active account |
32 |
| - msalInstance.addEventCallback(event => { |
33 |
| - if (event.eventType === EventType.LOGIN_SUCCESS && event.payload) { |
34 |
| - const result = event.payload as AuthenticationResult; |
35 |
| - if (result.account) { |
36 |
| - msalInstance.setActiveAccount(result.account); |
37 |
| - } |
38 |
| - } |
39 |
| - }); |
40 |
| - |
41 |
| - if (mounted.current) { |
42 |
| - const isLoggedIn = await checkLoggedIn(msalInstance).catch(e => { |
43 |
| - // Swallow check error but still allow app to render |
44 |
| - console.error("checkLoggedIn failed", e); |
45 |
| - return false; |
46 |
| - }); |
47 |
| - setLoggedIn(isLoggedIn); |
48 |
| - } |
49 |
| - } catch (e) { |
50 |
| - console.error("MSAL initialize failed", e); |
51 |
| - } finally { |
52 |
| - if (mounted.current) { |
53 |
| - setInitialized(true); |
54 |
| - } |
55 |
| - } |
56 |
| - }; |
57 |
| - init(); |
| 15 | + checkLoggedIn(instance) |
| 16 | + .then(isLoggedIn => { |
| 17 | + if (mounted.current) setLoggedIn(isLoggedIn); |
| 18 | + }) |
| 19 | + .catch(e => { |
| 20 | + console.error("checkLoggedIn failed", e); |
| 21 | + }); |
58 | 22 | return () => {
|
59 |
| - // On unmount: flag as unmounted so any pending async in init() doesn't call setState |
60 |
| - // This avoids React warnings about setting state on an unmounted component. |
61 | 23 | mounted.current = false;
|
62 | 24 | };
|
63 |
| - }, [msalInstance]); |
64 |
| - |
65 |
| - if (!initialized) { |
66 |
| - // Lightweight placeholder while MSAL initializes |
67 |
| - return <p>Loading authentication…</p>; |
68 |
| - } |
| 25 | + }, [instance]); |
69 | 26 |
|
70 | 27 | return (
|
71 |
| - <MsalProvider instance={msalInstance}> |
72 |
| - <LoginContext.Provider value={{ loggedIn, setLoggedIn }}> |
73 |
| - <Layout /> |
74 |
| - </LoginContext.Provider> |
75 |
| - </MsalProvider> |
| 28 | + <LoginContext.Provider value={{ loggedIn, setLoggedIn }}> |
| 29 | + <Layout /> |
| 30 | + </LoginContext.Provider> |
76 | 31 | );
|
77 | 32 | } else {
|
78 | 33 | return (
|
|
0 commit comments