angular2-jwt icon indicating copy to clipboard operation
angular2-jwt copied to clipboard

Error Using a Custom Options Factory Function

Open arthurfrancioni opened this issue 9 years ago • 3 comments

angular: 4.4.3 angular-jwt: 1.0.0-beta.9

I trying use a custom options factory function, but this error is occurring:

error

in my app.module.ts

export function jwtOptionsFactory(authService) {
     return {
           tokenGetter: () => {
                return localStorage.getItem('access_token');
           }
      }
}

@NgModule({
  imports:[ 
      HttpClientModule, 
      BrowserModule,
      JwtModule.forRoot({
            jwtOptionsProvider: {
                 provide: JWT_OPTIONS,
                 useFactory: jwtOptionsFactory,
                 deps: [AuthService]
            }
      }),
  ],
  providers: [ AuthService ],
  declarations: [ AppComponent ],
  bootstrap: [ AppComponent ]
})

arthurfrancioni avatar Oct 03 '17 20:10 arthurfrancioni

@arthurfrancioni I have the same error, did you manage to find a solution ?

fredrocheron avatar Dec 28 '17 10:12 fredrocheron

For future ref should someone stumble upon this thread. You can use a Custom Options Factory Function that way :

import { JwtModule, JWT_OPTIONS } from '@auth0/angular-jwt'; 
import { AuthService } from './auth/auth.service';

export function jwtOptionsFactory(authService) {
  return {
    tokenGetter: () => {
      return authService.accessToken;
    },
    throwNoTokenError: false,
		whitelistedDomains: ['http://localhost:3030', 'localhost:3030'], <-- YOUR API URL
  }
}

@NgModule({
  declarations: [
    CallbackComponent,
    LogoutComponent
  ],
  imports: [
    CommonModule,
    HttpClientModule,
    JwtModule.forRoot({
      jwtOptionsProvider: {
        provide: JWT_OPTIONS,
        useFactory: jwtOptionsFactory,
        deps: [AuthService]
      }
    }),
    ErrorsModule
  ],
  providers: [
    AuthGuardService,
    AuthService,
    NotificationService,
    SharedService,
    UserService,
    HttpService
  ]
})

export class CoreModule {
  constructor(
    @Optional() @SkipSelf() parentModule: CoreModule
  ) {
    throwIfAlreadyLoaded(parentModule, 'CoreModule');
  }
}

Couple things to know : 1- Options can only be passed in the function itself, contrary to what is shown in the docs. 2- The same docs also demonstrate how to get an access_token from localStorage. I avoided storing it for security reasons (xss, js attacks). Instead, It is dynamically retrieved from my angular authService (auth0.js) 3- If your SPA & API config is not right, you will get errors. I found a great summary of how things are supposed to play together there.

Hope this can help someone.

Standaa avatar Mar 28 '19 19:03 Standaa

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! 🙇‍♂️

stale[bot] avatar Oct 26 '19 03:10 stale[bot]