diff --git a/app/src/hooks/auth/authProvider.tsx b/app/src/hooks/auth/authProvider.tsx index 8e1676c..9d708af 100644 --- a/app/src/hooks/auth/authProvider.tsx +++ b/app/src/hooks/auth/authProvider.tsx @@ -10,21 +10,45 @@ interface AuthProviderProps { export const AuthProvider = ({ children }: AuthProviderProps ) => { const msalInstance = new PublicClientApplication(msalConfig); - // Default to using the first account if no account is active on page load - if (!msalInstance.getActiveAccount() && msalInstance.getAllAccounts().length > 0) { + msalInstance.initialize().then(() => { + msalInstance.handleRedirectPromise().then(handleResponse).catch((error) => { + console.error(error); + }) + }).catch((error) => { + console.log('handleRedirectPromise: ' + error) + }) + + const handleResponse = () => { + if (!msalInstance.getActiveAccount() && msalInstance.getAllAccounts().length > 0) { // Account selection logic is app dependent. Adjust as needed for different use cases. msalInstance.setActiveAccount(msalInstance.getAllAccounts()[0]); + } + + msalInstance.addEventCallback((event) => { + const authenticationResult = event.payload as AuthenticationResult; + const account = authenticationResult?.account; + + if (event.eventType === EventType.LOGIN_SUCCESS && account) { + msalInstance.setActiveAccount(account); + } + }); } - // Listen for sign-in event and set active account - msalInstance.addEventCallback((event) => { - const authenticationResult = event.payload as AuthenticationResult; - const account = authenticationResult?.account; + // Default to using the first account if no account is active on page load + // if (!msalInstance.getActiveAccount() && msalInstance.getAllAccounts().length > 0) { + // // Account selection logic is app dependent. Adjust as needed for different use cases. + // msalInstance.setActiveAccount(msalInstance.getAllAccounts()[0]); + // } - if (event.eventType === EventType.LOGIN_SUCCESS && account) { - msalInstance.setActiveAccount(account); - } - }); + // Listen for sign-in event and set active account + // msalInstance.addEventCallback((event) => { + // const authenticationResult = event.payload as AuthenticationResult; + // const account = authenticationResult?.account; + + // if (event.eventType === EventType.LOGIN_SUCCESS && account) { + // msalInstance.setActiveAccount(account); + // } + // }); return {children}