0

I need to pass the access token to a service each time is obtained, so this is what I did:

export class AppComponent implements OnInit {
   
  constructor(
    private eventService: PublicEventsService,
    private oidcSecurityService: OidcSecurityService
  ) { }

  ngOnInit(): void {
    
    this.eventService.registerForEvents().pipe(
      filter(notification => notification.type === EventTypes.NewAuthenticationResult 
        && (notification.value as AuthStateResult)?.isAuthenticated),
      withLatestFrom(this.oidcSecurityService.getAccessToken()),
      map(([_, accessToken]) => accessToken)
    )
    .subscribe(async accessToken => {
      console.log('New access token:', accessToken);
      // This behaves inconsistently for me, sometimes the accessToken is valid, sometimes is null.
    });
  }

}

The accessToken is sometimes null, sometimes valid. What am I missing?

Thanks.

Angular: version 15
angular-auth-oidc-client: 15.0.3
Paul
  • 1,224
  • 2
  • 14
  • 31

0 Answers0