1

Im using the @azure/msal-browser and @azure/msal-angular packages in order to simply retrieve an MSAL token for my signed in users. Since all I really need is the token I have only provided the MsalService and MSAL_INSTANCE in my app.module.ts. I am using the redirect flow and have chosen to subscribe to the .handleRedirectObservable() method myself instead of using the MsalRedirectComponent (so that I know exactly when the token is available).

My problem is that when the redirect happens and i am forwarded back to my application there is a period of time where my app is running but the redirect interaction still hasnt quite finished. If I allow for my app to actually start running at this point there is a chance that It will try to use the token before it is truly there and cause errors. So instead I am having to define a method called isInteractionInProgress and call that in my app.component.ts before actually starting execution of my app:

  public isInteractionInProgress(): boolean {
    for (let i = 0; i < sessionStorage.length; i+= 1) {
      if (sessionStorage.getItem(sessionStorage.key(i)) === 'interaction_in_progress') {
        return true;
      }
    }
    return false;
  }

Since I dont know a reliable way to generate the key that the msal-angular library uses to store the 'interaction_in_progress' value im having to iterate through the session storage myself and look for this key. If it exists then my app knows an MSAL token is on the way and it needs to wait.

Is there a better solution for this? It almost seems like this is supposed to be doing what I need:

this.msalBroadcastService.inProgress$
      .pipe(
        filter((status: InteractionStatus) => status === InteractionStatus.None),
        takeUntil(this._destroying$)
      )
      .subscribe(() => {
        // Do user account/UI functions here
      })

But all it does is activate on 'none' twice and the second 'none' seems to be when the token is actually available but im not sure if that is true.

So is there a reliable way to know when the redirect is actually happening? Thanks!

skyleguy
  • 979
  • 3
  • 19
  • 35

1 Answers1

0

It appears that you have already subscribed to handleRedirectObservable() over MsalRedirectComponent which is a recommended approach.

Read more here:

https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-angular/docs/v2-docs/redirects.md

https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-angular/docs/v2-docs/redirects.md

Rutha
  • 751
  • 3
  • 7
  • 2
    right, i wanted to subscribe to handleRedirectObservable so that the second the token exists i could send it to my other components. whereas with the MsalRedirectComponent approach I dont ever have access to the msal token unless i want to scavenge through the sessionStorage to pull it out – skyleguy Jul 02 '21 at 14:33