angularmsal-angularangular-standalone-components

How to configure Angular v15.2 standalone application with MSAL


Before in ngModule base angular application you use to add MsalRedirectComponent in the bootstrap property of AppModule :

bootstrap: [AppComponent, MsalRedirectComponent]

Now in V15.2 the AppModule have been replace by the boostrapApplication function in main.ts :

bootstrapApplication(AppComponent, {
    providers: [
        importProvidersFrom(
            BrowserModule,
            AppRoutingModule,
           ),       
        provideAnimations(),
        provideHttpClient(withInterceptorsFromDi())
    ]
})
    .catch(err => console.error(err));

How to add the MsalRedirectComponent bootstrap ?

I try to add an other call after the first :

bootstrapApplication(MsalRedirectComponent, {
    providers: [
        importProvidersFrom(
            BrowserModule,
            AppRoutingModule,),
        provideHttpClient(withInterceptorsFromDi())
    ]
})
    .catch(err => console.error(err));

But this didn't work, the redirect didn't fired.


Solution

  • For information, i post my working solution : I decide to not use the extends class, instead i use the handleRedirectObservable build in Msal property.

    In main.ts I add the router for auth:

    bootstrapApplication(AppComponent, {
    
        providers: [
             provideRouter([{ path: 'auth', loadComponent: () => import('@azure/msal-angular').then(mod => mod.MsalRedirectComponent) }]),
    
            importProvidersFrom(
                BrowserModule,
                AppRoutingModule,
               ),       
            provideAnimations(),
            provideHttpClient(withInterceptorsFromDi())
        ]
    })
        .catch(err => console.error(err));
    
    
    @Component({
      selector: 'app-root',
      templateUrl: 'app.component.html',
      styles: [],
      standalone: true,
      imports: [CommonModule,RouterOutlet]
    })
    export class AppComponent implements OnInit, OnDestroy {
    
      constructor(  
        private msalService: MsalService,
      ) {
         }
    
      ngOnInit(): void {
          this.msalService.handleRedirectObservable().subscribe();
      }
    }