Angular2-jwt: Webpack for AOT can't handle lambda expressions

Created on 9 Aug 2017  ·  9Comments  ·  Source: auth0/angular2-jwt

This is for the v1.0 branch -- it is a known issue that lambda expressions won't work when compiling with Webpack for AOT as that process reguires named exported functions. Thus this line in jwt.interceptor.ts and jwt.interceptor.d.ts causes an error:

tokenGetter?: () => string | Promise;

The error at runtime is

this.tokenGetter is not a function.

A work-around is to replace the lambda expression with a named exported function. I'm not sure how that would work in a d.ts file, hopefully you can figure that out. Thanks.

stale

Most helpful comment

I fixed it like this

export function getAccessToken() {
  return localStorage.getItem('access_token');
}

JwtModule.forRoot({
      config: {
        tokenGetter: (getAccessToken),
        whitelistedDomains: ['localhost:44314']
      }
    })

All 9 comments

I'm facing
same problem
Can someone help us please

screen shot 2017-08-11 at 2 56 22 am

screen shot 2017-08-11 at 2 43 11 am

I fixed it like this

export function getAccessToken() {
  return localStorage.getItem('access_token');
}

JwtModule.forRoot({
      config: {
        tokenGetter: (getAccessToken),
        whitelistedDomains: ['localhost:44314']
      }
    })

okay cool!

i will check out on morning and let you know,
if works on my side.

cool, just remember to change the whitelistedDomains in the example I pasted

okay cool!

Thanks for your suggestion -- interesting idea to use a string. Alas I'm still getting this error when I try it:

TypeError: this.tokenGetter is not a function
    at JwtInterceptor.intercept (vendor.bundle.js:1)
    at HttpInterceptorHandler.handle (

I should clarify that this is at run time. I'm not seeing the compile time error referenced above.

@jculverwell's solution didn't work for me either. It gives me this.tokenGetter is not a function.

@chenkie do you have a solution for this?

This worked ok for me, in same module:
Before module declaration:

export function getJwtToken(): string {
  return localStorage.getItem("access_token");
}

Module imports config:

JwtModule.forRoot({
      config: {
        tokenGetter: getJwtToken,
        whitelistedDomains: ['localhost:8000']
      }
    })

Angular 4.3.6
@auth0/angular-jwt 1.0.0-beta.9

Without a return type declaration the build wasn't passing for me.

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

Related issues

JaxonWright picture JaxonWright  ·  4Comments

Tirka picture Tirka  ·  3Comments

guillaume-skwid picture guillaume-skwid  ·  5Comments

kolkov picture kolkov  ·  3Comments

sfabriece picture sfabriece  ·  4Comments