AzureAD / microsoft-authentication-library-for-js

Microsoft Authentication Library (MSAL) for JS
http://aka.ms/aadv2
MIT License
3.64k stars 2.65k forks source link

MsalService.handleRedirectObservable not returning accountState/state on error #4371

Open hannonjohn opened 2 years ago

hannonjohn commented 2 years ago

Core Library

MSAL.js v2 (@azure/msal-browser)

Wrapper Library

MSAL Angular (@azure/msal-angular)

Description

I'm migrating from v1 > v2 and using the redirect flows.

I am passing custom state via the state parameter in the RedirectRequest. For example, I am passing the B2C userflow name.

The v1 MsalService.handleRedirectCallback((error, response) => {}) was returning accountState in response when an error occurred. This was convenient. For example, I could distinguish between a sign-in or sign-up userflow and show a different error message to the user.

But when the v2 MsalService.handleRedirectObservable errors I can't get this state - AuthError param is all I have to work with and it doesn't have an accountState / state property. This is inconvenient.

Source

External (Customer)

MarianNikolov commented 1 year ago

Hi, facing the same problem with @azure/msal-browser 2.37.1. Any further information or workaround on this?

hannonjohn commented 1 year ago

Hi, facing the same problem with @azure/msal-browser 2.37.1. Any further information or workaround on this?

Hi MarianNikolov, in the end, I went with my own localStorage solution.

So, before the redirect:

this.b2cLocalStorageService.setMsalRedirectState(state); // localStorage.setItem('key')

this.msalService.loginRedirect(redirectRequest);

And on a redirect error:

this.msalService.handleRedirectObservable().subscribe({
  next: (res: AuthenticationResult) => {
    // ...
  },
  error: (error: AuthError) => {
    const state = this.b2cLocalStorageService.getMsalRedirectState(); // localStorage.getItem('key')

    const errorType = this.b2cHelperService.getErrorType(error);

    switch (state.authFlowType) {
      case AuthFlowType.Signin:
        this.store.dispatch(
          B2CActions.signinFailure({
            errorType,
            error
          })
        );
        break;
      case AuthFlowType.Signup:
        // ...
        break;
      case AuthFlowType.ResetPwd:
        // ...
        break;
    }
  }
});
jeremy-daley-kr commented 9 months ago

You can see here that msal-browser is specifically emitting a null value for the payload param when there's an error: https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/b560a06b7389f964fcc78092e10df42e5a74d2de/lib/msal-browser/src/controllers/StandardController.ts#L434

I would like to know if there's a specific reason for this.

Following the history on that specific line, back to ClientApplication.ts, I'm wondering if someone from this list knows: @shoatman @tnorling @jasonnutter @jo-arroyo

Thanks!