Angular2-jwt: Configuration with factory doesn't work

Created on 12 Apr 2020  路  7Comments  路  Source: auth0/angular2-jwt

Hi there, i'm using "@auth0/angular-jwt": "^4.0.0",.
I followed documentation:
https://github.com/auth0/angular2-jwt#configuration-for-ionic-2

But factory doesn't work.
Doesn't work with ionic storage neither localstorage.

export function jwtOptionsFactory(storage: Storage) { return { tokenGetter: () => { // return storage.get('access_token'); return localStorage.getItem('access_token'); } } } ... JwtModule.forRoot({ jwtOptionsProvider: { provide: JWT_OPTIONS, useFactory: jwtOptionsFactory, deps: [Storage] } }),

Without factory, it works fine:
export function tokenGetter() { return localStorage.getItem('access_token'); } ... JwtModule.forRoot({ config: { tokenGetter: tokenGetter, whitelistedDomains: endpoint.jwtWhitelist } }),

documentation has pull request

All 7 comments

I see that JWT_OPTIONS declaration is:
export declare const JWT_OPTIONS: InjectionToken<unknown>;
I don't know if it's an issue but it seems weird...

Tried with @auth0/[email protected].
Factories don't work either.
Maybe my config is bad.
JwtModule.forRoot({ jwtOptionsProvider: { provide: JWT_OPTIONS, useFactory: jwtOptionsFactory, deps: [Storage] } }),
Can anyone help me ?

Hey @pairmix thanks for reporting this issue.

looks like you did not specify a whitelisted domain. You can do that configuring the whitelistedDomains array in the jwtOptionsFactory.

export function jwtOptionsFactory(storage) {
  return {
    tokenGetter: () => {
      return storage.get("access_token");
    },
    whitelistedDomains: ["localhost:3000"],
  };
}

This is not clear from the documentation and I will be working on clarifying this.

Thank you very much @Sambego , you saved my life!!!

The documentation has been updated to be clearer about configuring the options with a jwtOptionsFactory.

Here the name of the array seems to be allowedDomains but that doesn't work. Am I missing something or should that also be fixed as part of this?

@oguzhanogreden the name of that array did indeed change to allowedDomains in version v5.0.0, are you using the latest version v5.0.1?

If something isn't working for you, please open an new issue with some more details.

Was this page helpful?
0 / 5 - 0 ratings