MSAL authentication in Angular Logout redirect problem

53 Views Asked by At

Working on angular app with MSAL authentication and configured it with MSAL config factory and implemented in providers of the app module. I have provided a post logout redirect uri in the publicclientapplication(). When I logout for a small instance it redirects to the given page, but wont stay at it and goes back page before logout.

export function MSALInstanceFactory(): IPublicClientApplication {
    return new PublicClientApplication({
      auth: {
        clientId: "",
        authority:"",
        redirectUri: "http://localhost:4200",
        postLogoutRedirectUri: "http://localhost:4200/home",
      },
      cache: {
        cacheLocation: BrowserCacheLocation.LocalStorage
      },
    });
  }
  
  export function MSALInterceptorConfigFactory(): MsalInterceptorConfiguration {
    const protectedResourceMap = new Map<string, Array<string>>();
    protectedResourceMap.set("https://graph.microsoft.com/v1.0/me", ["user.read"]);
  
    return {
      interactionType: InteractionType.Redirect,
      protectedResourceMap,
    };
  }
  
  export function MSALGuardConfigFactory(): MsalGuardConfiguration {
    return { 
      interactionType: InteractionType.Redirect,
      authRequest: {
        scopes: ['user.read']
      },
      loginFailedRoute: "./login-failed"
    };
  }

app.module.ts

@NgModule({
  declarations: [....],
  imports: [....],
  providers: [{
    provide: HTTP_INTERCEPTORS,
    useClass: MsalInterceptor,
    multi: true
  },
  {
    provide: MSAL_INSTANCE,
    useFactory: MSALInstanceFactory
  },
  {
    provide: MSAL_GUARD_CONFIG,
    useFactory: MSALGuardConfigFactory
  },
  {
    provide: MSAL_INTERCEPTOR_CONFIG,
    useFactory: MSALInterceptorConfigFactory
  },
  MsalGuard,
  MsalBroadcastService,
  MsalService
],
  bootstrap: [AppComponent]
})

Login page

export class NavbarComponent implements OnInit, OnDestroy {

  isIframe = false;
  loginDisplay = false;
  private readonly _destroying$ = new Subject<void>();
  profile!: AccountInfo

  constructor(private authService: MsalService, 
    private broadcastService: MsalBroadcastService, 
    private http: HttpClient,
    @Inject(MSAL_GUARD_CONFIG) private msalGuardConfig: MsalGuardConfiguration){}

  ngOnInit(): void {
    this.authService.initialize().subscribe(()=>{
    this.isIframe = window !== window.parent && !window.opener;
    this.broadcastService.inProgress$
    .pipe(
      filter((status: InteractionStatus) => status === InteractionStatus.None),
      map(() => {
        const instance: IPublicClientApplication = this.authService.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;
    }),
      takeUntil(this._destroying$)
    )
    .subscribe((account) => {
      this.getProfile(account)
      this.setLoginDisplay();
    })
    })
  }

  setLoginDisplay() {
    this.loginDisplay = this.authService.instance.getAllAccounts().length > 0;
  }

  login(){
    if (this.msalGuardConfig.authRequest){
      this.authService.loginPopup({...this.msalGuardConfig.authRequest} as PopupRequest);
    } else {
      this.authService.loginPopup();
    }
  }
  logout(){
    this.authService.logoutPopup()
  }
  ngOnDestroy(): void {
    this._destroying$.next(undefined);
    this._destroying$.complete();
  }

  getProfile(account: AccountInfo) {
    this.profile = account
  }

app-roots.module.ts

const routes: Routes = [
  {path: 'home', component: HomeComponentComponent},
  {path:  'user',canActivate: [MsalGuard], canActivateChild: [MsalGuard], children: [
    {path: 'create', component: CreateUserComponent},
    {path: 'configure', component: ConfigureGroupComponent}  
  ]}

I configured correctly upto my knowledge but wont redirect properly. And I am expecting my application to my home component which is not protected by MSAL Guard.

0

There are 0 best solutions below