I am trying with MSAL (@azure/msal-angular) for Azure Signin and new to angular .I need to get username after logged in and display it in UI.Thanks in advance
You can retrieve the user from MsalService
, for example with:
const accountInfo: AccountInfo | null = this.msalService.instance.getActiveAccount();
It's recommended to wait until the authentication process is complete before you invoke this. This can be done by checking MsalBroadcastService
:
const account: Observable<AccountInfo | null> = this.msalBroadcastService
.inProgress$
.pipe(
filter(status => status == InteractionStatus.None),
map(() => {
const instance: IPublicClientApplication = this.msalService.instance;
const activeAccount: AccountInfo | null = instance.getActiveAccount();
const accounts: AccountInfo[] = instance.getAllAccounts();
if (activeAccount != null) return activeAccount;
if (accounts.length > 0) {
const [firstAccount] = accounts;
instance.setActiveAccount(firstAccount);
return firstAccount;
}
return null;
})
);
});