Skip to content

Commit da77913

Browse files
authored
fix: prevent undefined account error when logging out (#3082)
1 parent daf0cf9 commit da77913

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<script type="module">
1515
// import { registerMgtComponents, Providers, MockProvider, Msal2Provider } from 'https://unpkg.com/@microsoft/mgt@next/dist/bundle/mgt.es6.js';
16-
import { registerMgtComponents, Providers, MockProvider, Msal2Provider } from './packages/mgt/dist/bundle/mgt.es6.js';
16+
import { registerMgtComponents, Providers, MockProvider, Msal2Provider } from './packages/mgt/dist/bundle/mgt.js';
1717
// Providers.globalProvider = new MockProvider(true);
1818
Providers.globalProvider = new Msal2Provider({
1919
clientId: '2dfea037-938a-4ed8-9b35-c05708a1b241',

packages/mgt-components/src/components/mgt-login/mgt-login.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,12 @@ export class MgtLogin extends MgtTemplatedTaskComponent {
241241
}
242242

243243
const provider = Providers.globalProvider;
244-
if (provider?.isMultiAccountSupportedAndEnabled) {
245-
localStorage.removeItem(provider.getActiveAccount().id + this._userDetailsKey);
246-
}
247244
if (provider?.logout) {
245+
const activeAccount = provider.getActiveAccount();
248246
await provider.logout();
249247
this.userDetails = null;
250248
if (provider.isMultiAccountSupportedAndEnabled) {
251-
localStorage.removeItem(provider.getActiveAccount().id + this._userDetailsKey);
249+
localStorage.removeItem(activeAccount?.id + this._userDetailsKey);
252250
}
253251
this.hideFlyout();
254252
this.fireCustomEvent('logoutCompleted');

packages/providers/mgt-msal2-provider/src/Msal2Provider.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -531,12 +531,16 @@ export class Msal2Provider extends IProvider {
531531
*/
532532
public getActiveAccount() {
533533
const account = this._publicClientApplication.getActiveAccount();
534-
return {
535-
name: account.name,
536-
mail: account.username,
537-
id: account.homeAccountId,
538-
tenantId: account.tenantId
539-
} as IProviderAccount;
534+
535+
if (account) {
536+
return {
537+
name: account.name,
538+
mail: account.username,
539+
id: account.homeAccountId,
540+
tenantId: account.tenantId
541+
} as IProviderAccount;
542+
}
543+
return undefined;
540544
}
541545

542546
/**

0 commit comments

Comments
 (0)