I have a Angular app (v11.2.0) that uses MSAL for authentication. I recently upgraded to v2 of the library (@azure/msal-angular - ^2.1.1, @azure/msal-browser - ^2.22.0), and it took some refactoring due to MSAL changes. It's mostly working as before with the exception of when it's authenticating a user and acquiring an authentication response: the process loops roughly 3 times before successfully getting an auth response, visibly refreshing the app each time. The approximate flow I'm seeing is as follows:
- Loop 1
- Navigate to app
- User not authenticated
- Handle redirect start
- Handle redirect promise called but there is no interaction in progress, returning null
- Handle redirect end
- Login start
- null authentication result received
- Loop 2
- Navigate to app
- User not authenticated
- Handle redirect start
- Loop 3
- Navigate to app
- User not authenticated
- Handle redirect start
- Info - in acquire token call
- Login success
- Handle redirect end
- Acquire token start
- Authentication result received
Authentication is initiated and handled in my app.component.ts:
ngOnInit() {
this.msalBroadcastService.inProgress$
.pipe(
filter((status: InteractionStatus) => status === InteractionStatus.None)
)
.subscribe(async () => {
if (!this.authenticated) {
await this.logIn();
}
})
this.msalService.handleRedirectObservable().subscribe({
next: (result: AuthenticationResult) => {
if (!this.msalService.instance.getActiveAccount() &&
this.msalService.instance.getAllAccounts().length > 0) {
this.msalService.instance.setActiveAccount(result.account);
}
},
error: (error) => console.log(error)
});
}
async logIn() {
await this.msalService.instance.loginRedirect({
scopes: ['user.read', 'openid', 'profile'],
redirectUri: AppConfig.settings.authenticationconfig.redirectUri
});
};
get authenticated(): boolean {
return this.msalService.instance.getActiveAccount() ? true : false;
}
Has anyone experienced a similar situation or have any understanding of what's causing this behaviour and how to resolve it?