question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JwtHelperService does not work when using promise

See original GitHub issue

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”

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:12

github_iconTop GitHub Comments

3reactions
vukdavidcommented, Oct 4, 2019

Yes, it’s still an issue. 😦 Any luck with the fix?

3reactions
maciej-sielskicommented, Sep 10, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular - Getting error in Jwthelperservice - Stack Overflow
I am using Visual studio code and it is not throwing any syntax error. But getting error in console : Error: StaticInjectorError(AppModule)[ ...
Read more >
Developers - JwtHelperService does not work when using promise -
JwtHelperService does not work when using promise. ... I'm trying to use jwtOptionsFactory to handle getting token from ngrx store. The interceptor works...
Read more >
angular-jwt-async - npm Package Health Analysis - Snyk
JSON Web Token helper library for Angular. Forked from @auth0/angular-jwt. Allows for async promises for whitelist and blacklist For more information about ...
Read more >
@auth0/angular-jwt - npm
This library provides an HttpInterceptor which automatically attaches a JSON Web Token to HttpClient requests. This library does not have any ...
Read more >
error ts2366: function lacks ending return statement ... - You.com
You need to return the whole Promise chain. As is, the setAdress function doesn't return anything. You should also consider fixing the spelling...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found