Skip to content

Commit

Permalink
fix: prevent undefined account error when logging out (#3082)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnickii authored Mar 8, 2024
1 parent daf0cf9 commit da77913
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<script type="module">
// import { registerMgtComponents, Providers, MockProvider, Msal2Provider } from 'https://unpkg.com/@microsoft/mgt@next/dist/bundle/mgt.es6.js';
import { registerMgtComponents, Providers, MockProvider, Msal2Provider } from './packages/mgt/dist/bundle/mgt.es6.js';
import { registerMgtComponents, Providers, MockProvider, Msal2Provider } from './packages/mgt/dist/bundle/mgt.js';
// Providers.globalProvider = new MockProvider(true);
Providers.globalProvider = new Msal2Provider({
clientId: '2dfea037-938a-4ed8-9b35-c05708a1b241',
Expand Down
6 changes: 2 additions & 4 deletions packages/mgt-components/src/components/mgt-login/mgt-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,12 @@ export class MgtLogin extends MgtTemplatedTaskComponent {
}

const provider = Providers.globalProvider;
if (provider?.isMultiAccountSupportedAndEnabled) {
localStorage.removeItem(provider.getActiveAccount().id + this._userDetailsKey);
}
if (provider?.logout) {
const activeAccount = provider.getActiveAccount();
await provider.logout();
this.userDetails = null;
if (provider.isMultiAccountSupportedAndEnabled) {
localStorage.removeItem(provider.getActiveAccount().id + this._userDetailsKey);
localStorage.removeItem(activeAccount?.id + this._userDetailsKey);
}
this.hideFlyout();
this.fireCustomEvent('logoutCompleted');
Expand Down
16 changes: 10 additions & 6 deletions packages/providers/mgt-msal2-provider/src/Msal2Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,12 +531,16 @@ export class Msal2Provider extends IProvider {
*/
public getActiveAccount() {
const account = this._publicClientApplication.getActiveAccount();
return {
name: account.name,
mail: account.username,
id: account.homeAccountId,
tenantId: account.tenantId
} as IProviderAccount;

if (account) {
return {
name: account.name,
mail: account.username,
id: account.homeAccountId,
tenantId: account.tenantId
} as IProviderAccount;
}
return undefined;
}

/**
Expand Down

0 comments on commit da77913

Please sign in to comment.