Angular2-jwt: JwtHelperService does not work when using promise

Created on 19 Jun 2019  ·  11Comments  ·  Source: auth0/angular2-jwt

Description

I'm trying to use jwtOptionsFactory to handle getting token from ngrx store.

The interceptor works just fine, but when I try to us JwtHelperService without passing token (I guess it uses tokengetter by default) or with passing token via this.jwtHelperService.tokenGetter() does not work because my factory return promise while the method from service tries run split method on promise, not on string...

My code:

function jwtOptionsFactory(store: Store<any>): object {
  return {
    whitelistedDomains: ['localhost:5500'],
    tokenGetter: () => {
      return store.select(FromAppState.selectAuthToken)
        .pipe(take(1))
        .toPromise();
    }
  };
}
@NgModule({
  // ...
  imports: [
    // ...
    JwtModule.forRoot({
      jwtOptionsProvider: {
        provide: JWT_OPTIONS,
        useFactory: jwtOptionsFactory,
        deps: [Store]
      }
    }),
    // ...
  ]
  bootstrap: [AppComponent]
})
export class AppModule {}
  constructor(private dialogService: DialogService,
              private jwtHelperService: JwtHelperService) { }

  ngOnInit(): void {
    console.log(this.jwtHelperService.tokenGetter()); // <= return promise, not string
    console.log(this.jwtHelperService.decodeToken(this.jwtHelperService.tokenGetter()));
    console.log(this.jwtHelperService.isTokenExpired(this.jwtHelperService.tokenGetter()));
    console.log(this.jwtHelperService.getTokenExpirationDate(this.jwtHelperService.tokenGetter()));
  }

error:
image

Please fix this

Also it would be great if this would also supports Observable

Prerequisites

Environment

Please provide the following:

  • Version of angular2-jwt being used: "@auth0/angular-jwt": "^2.1.0",
  • Version of the platform or framework used, if applicable: "@angular/core": "~8.0.0"
bug stale help wanted

Most helpful comment

Yes, it's still an issue. :(
Any luck with the fix?

All 11 comments

@spiotr12 Yes I just yesterday noticed that this was broken myself, thanks for raising the issue. I've put it into our backlog to get looked at.

I have the same problem in version 3.0. Any luck with bug fix?

Yes, it's still an issue. :(
Any luck with the fix?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you have not received a response for our team (apologies for the delay) and this is still a blocker, please reply with additional information or just a ping. Thank you for your contribution! 🙇‍♂️

Was this fixed or just forgotten?

It hasn't been fixed but is in our backlog. We'd appreciate any help if you fancy having a go at getting this working.

+1

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you have not received a response for our team (apologies for the delay) and this is still a blocker, please reply with additional information or just a ping. Thank you for your contribution! 🙇‍♂️

Any news on this one?

Until the PR is merged, the workaround can be installed via github package registry: "@smasala/auth0-angular-jwt": "^4.2.1",

https://github.com/smasala/angular2-jwt/packages/558340

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you have not received a response for our team (apologies for the delay) and this is still a blocker, please reply with additional information or just a ping. Thank you for your contribution! 🙇‍♂️

Was this page helpful?
0 / 5 - 0 ratings