Skip to content

JwtHelperService does not work when using promise #606

@spiotr12

Description

@spiotr12

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions