BrowserAuthError: interaction_in_progress - Unable to fix, regardles of solutions found
Asked Answered
M

2

9

I'm implementing security for the applications at the company I'm working at right now. I'm using @azure/[email protected], @azure/[email protected]. I followed the example found here and got it working for the first application. I went on to implement it for the next application, which is basically the same one, just talks to a different api, but the complexity is the same. After possibly doing something wrong I keep getting the error:

core.js:6157 ERROR Error: Uncaught (in promise): BrowserAuthError: interaction_in_progress: Interaction is currently in progress. Please ensure that this interaction has been completed before calling an interactive API.  For more visit: aka.ms/msaljs/browser-errors.
BrowserAuthError: interaction_in_progress: Interaction is currently in progress. Please ensure that this interaction has been completed before calling an interactive API.  For more visit: aka.ms/msaljs/browser-errors.

I found several other posts that said to clear the caches, storages etc... But none of it works. All it does is prompt me to log in again, only to fill in the sessionStorage back with the same entries, amongst them the entry that says msal.442edcf7-9e0c-469b-93fb-daa1839724bd.interaction.status interaction_in_progress. As far as I know I've tried everthing I've found. Also the solution from AzureAD themselves doesn't work.

I'm very new to this so I might have missed something simple, if so, my apologies.

My code can be found below.

Angular version

11.0.2

app.module.ts

import {
  MsalBroadcastService,
  MsalGuard,
  MsalInterceptor,
  MsalModule, MsalRedirectComponent,
  MsalService
} from "@azure/msal-angular";
import {BrowserCacheLocation, InteractionType, LogLevel, PublicClientApplication} from '@azure/msal-browser';


const isIE = window.navigator.userAgent.indexOf('MSIE ') > -1 || window.navigator.userAgent.indexOf('Trident/') > -1;
@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    RibonComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    ...,
    MsalModule.forRoot(
      new PublicClientApplication({ // MSAL Configuration
        auth: {
          clientId: "442edcf7-...",
          authority: "https://login.microsoftonline.com/81fa766e-...",
          redirectUri: window.location.origin,
        },
        cache: {
          cacheLocation : BrowserCacheLocation.LocalStorage,
          storeAuthStateInCookie: false, // set to true for IE 11
        },
        system: {
          loggerOptions: {
            loggerCallback: (level: LogLevel, message: string, containsPii: boolean): void => {
              if (containsPii) {
                return;
              }
              switch (level) {
                case LogLevel.Error:
                  console.error(message);
                  return;
                case LogLevel.Info:
                  console.info(message);
                  return;
                case LogLevel.Verbose:
                  console.debug(message);
                  return;
                case LogLevel.Warning:
                  console.warn(message);
                  return;
              }
              },
            piiLoggingEnabled: false
          }
        }
      }), {
        interactionType: InteractionType.Popup, // MSAL Guard Configuration
      }, {
        protectedResourceMap: new Map([
          [ 'http://localhost:8400/', ['api://442edcf7-9e0c-469b-93fb-daa1839724bd/acces_as_user/Acces-user']],
          [ 'https://quality-score-dev-pcq-dev.bravo-apps.volvocars.biz/', ['api://442edcf7-9e0c-469b-93fb-daa1839724bd/acces_as_user/Acces-user']]
        ]),
        interactionType: InteractionType.Redirect // MSAL Interceptor Configuration
      }
    )
  ],
  providers: [
    EnvServiceProvider,
    {
      provide: MatPaginatorIntl,
      useClass: MatPaginatorIntlTranslator
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: MsalInterceptor,
      multi: true
    },
    MsalService,
    MsalGuard,
    MsalBroadcastService,
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit, OnDestroy{
  title = 'Quality score';

  isIframe = false;
  loginDisplay = false;

  activeUser = '';

  private readonly onDestroy$ = new Subject<void>()

  constructor(
    @Inject(MSAL_GUARD_CONFIG) private msalGuardConfig: MsalGuardConfiguration,
    private languageService: LanguageService,
    private authService: MsalService,
    private msalBroadcastService: MsalBroadcastService,
    private location: Location,
    private userService: UserService
  ){}

  ngOnInit(): void {
    const currentPath = this.location.path();
    // Dont perform nav if in iframe or popup, other than for front-channel logout
    this.isIframe = BrowserUtils.isInIframe() && !window.opener && currentPath.indexOf("logout") < 0; // Remove this line to use Angular Universal

    this.msalBroadcastService.inProgress$
      .pipe(
        filter((status: InteractionStatus) => status === InteractionStatus.None),
        takeUntil(this.onDestroy$)
      )
      .subscribe(() => {
        this.setLoginDisplay();
        this.checkAndSetActiveAccount();
      })
  }

  ngOnDestroy() {
    this.onDestroy$.next();
  }

  setLoginDisplay() {
    this.loginDisplay = this.authService.instance.getAllAccounts().length > 0;
  }
  checkAndSetActiveAccount(){
    /**
     * If no active account set but there are accounts signed in, sets first account to active account
     * To use active account set here, subscribe to inProgress$ first in your component
     * Note: Basic usage demonstrated. Your app may require more complicated account selection logic
     */
    let activeAccount = this.authService.instance.getActiveAccount();

    if (!activeAccount && this.authService.instance.getAllAccounts().length > 0) {
      let accounts = this.authService.instance.getAllAccounts();
      this.authService.instance.setActiveAccount(accounts[0]);
    }

    if(activeAccount) {
      this.userService.activeUserName = activeAccount.name;
      this.activeUser = activeAccount.name;
    }
  }

  logout(): void{
    this.authService.logoutRedirect();
  }
}

I'm really lost and have no idea what I have done wrong. From my understanding there is a login process that was interupted, probably by me leaving the login screen, but now I have no idea how to "finish it up" or complete the process.

Update

I tried copying the LocalStorage values from the working application and I got it to work. Refreshing works and no errors appear, but when I logout and after it prompted me to login again and I do, then it's right back to the start again.

Solution update

I've had a breakthrough. If I change the login type to Popup and handle it this way, it's fixed. I can login and logout without any issues. However if I then change it back to Redirect, it's broken again. So for now I'll keep it on Popup. Simple solution, but I hadn't thought of it because I assumed the issue would be occur there as well.

Mordant answered 10/8, 2021 at 12:17 Comment(2)
I'm running into the same issue super frustrating thinking of switching libraries.Jarred
Yes, I was also same issue and when I replace msalInstance.logoutRedirect with msalInstance.logoutPopup it's working correctly.Avigation
C
6

I can't tell by the code provided, but I had a similar issue. On top of that I was getting the following error in the console: Error: The selector "app-redirect" did not match any elements

This lead me to the following post: https://github.com/AzureAD/microsoft-authentication-library-for-js/issues/3114#issuecomment-788394239

I added <app-redirect></app-redirect> to the index.html page, below app-root as suggested and this seemed to sort both issues.

Chura answered 11/4, 2022 at 12:20 Comment(3)
That is indeed the solution I found after a while. I forgot to update te solution, so thanks for providing it!Mordant
Thanks for the resolution, I have tried many ways and failed to resolve this for the last one week. With the github link which you provide, this issue is resolved. Thanks againSiskin
Thank you! This was the exact fix I neededMurillo
M
0

"Workaround" fix

Make your login type Popup. Dumb of me not to think about that

Mordant answered 19/8, 2021 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.