Refactoring

This commit is contained in:
2025-01-19 15:52:27 -06:00
parent db4af1e3ed
commit b85d0a6b22

View File

@@ -10,13 +10,20 @@ interface AuthProviderProps {
export const AuthProvider = ({ children }: AuthProviderProps ) => { export const AuthProvider = ({ children }: AuthProviderProps ) => {
const msalInstance = new PublicClientApplication(msalConfig); const msalInstance = new PublicClientApplication(msalConfig);
// Default to using the first account if no account is active on page load 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) { if (!msalInstance.getActiveAccount() && msalInstance.getAllAccounts().length > 0) {
// Account selection logic is app dependent. Adjust as needed for different use cases. // Account selection logic is app dependent. Adjust as needed for different use cases.
msalInstance.setActiveAccount(msalInstance.getAllAccounts()[0]); msalInstance.setActiveAccount(msalInstance.getAllAccounts()[0]);
} }
// Listen for sign-in event and set active account
msalInstance.addEventCallback((event) => { msalInstance.addEventCallback((event) => {
const authenticationResult = event.payload as AuthenticationResult; const authenticationResult = event.payload as AuthenticationResult;
const account = authenticationResult?.account; const account = authenticationResult?.account;
@@ -25,6 +32,23 @@ export const AuthProvider = ({ children }: AuthProviderProps ) => {
msalInstance.setActiveAccount(account); msalInstance.setActiveAccount(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]);
// }
// 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 <MsalProvider instance={msalInstance}> return <MsalProvider instance={msalInstance}>
{children} {children}