Angular-auth-oidc-client: Support isAuthorized$: Observable<boolean>

Created on 11 Jul 2017  路  4Comments  路  Source: damienbod/angular-auth-oidc-client

In my component I have a property bound to the isAuthorized which remains false since authorizedCallback is async.

ngOnInit() {
    if (window.location.hash) {
      this.oidcSecurityService.authorizedCallback();
    }
    this.authenticated = this.oidcSecurityService.isAuthorized;
  }

The way I got around this is by subscribing to the onUserDataLoaded EventEmitter, but it feels like there should be an Observable that can emit specifically for this property.

Most helpful comment

The authorizedCallback method already emits the event that you have indicated: https://github.com/damienbod/angular-auth-oidc-client/blob/master/src/services/oidc.security.service.ts#L239

This event is emitted after that isAuthorized is set: https://github.com/damienbod/angular-auth-oidc-client/blob/master/src/services/oidc.security.service.ts#L229

So the following code in my opinion is correct:

ngOnInit() {
    if (typeof location !== "undefined" &&window.location.hash) {
      this.oidcSecurityService.authorizedCallback();

      this.oidcSecurityService.onUserDataLoaded.subscribe(() => { 
        this.authenticated = this.oidcSecurityService.isAuthorized;
      });
    }   
}

I think that another event is useless. Perhaps a more explanation of this event is needed in the docs.

All 4 comments

If your using the main component, you need to wait for an event.

you could wait for the onUserDataLoaded event.

this.oidcSecurityService.onUserDataLoaded.subscribe(() => { this.onUserDataLoaded(); });

And Implement a separate function and set your authenticated property.

Or if you use the this.oidcSecurityService.isAuthorized directly in the template, it will be updated when it is set.

@robisim74 @FabianGosebrink Do we need a new event for the isAuthorized?

The authorizedCallback method already emits the event that you have indicated: https://github.com/damienbod/angular-auth-oidc-client/blob/master/src/services/oidc.security.service.ts#L239

This event is emitted after that isAuthorized is set: https://github.com/damienbod/angular-auth-oidc-client/blob/master/src/services/oidc.security.service.ts#L229

So the following code in my opinion is correct:

ngOnInit() {
    if (typeof location !== "undefined" &&window.location.hash) {
      this.oidcSecurityService.authorizedCallback();

      this.oidcSecurityService.onUserDataLoaded.subscribe(() => { 
        this.authenticated = this.oidcSecurityService.isAuthorized;
      });
    }   
}

I think that another event is useless. Perhaps a more explanation of this event is needed in the docs.

fixing here

https://github.com/damienbod/angular-auth-oidc-client/issues/42

The onUserDataLoaded is only emitted after a login from a id_token token flow. This does not work with a id_token flow (which is anyway useless but has to be supported for OIDC certification) and also with a browser refresh

Updated 1.3.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vicver82 picture vicver82  路  4Comments

hannesrohde picture hannesrohde  路  3Comments

yelhouti picture yelhouti  路  4Comments

brentos99 picture brentos99  路  4Comments

Expelz picture Expelz  路  4Comments