Error Using a Custom Options Factory Function
angular: 4.4.3 angular-jwt: 1.0.0-beta.9
I trying use a custom options factory function, but this error is occurring:

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 I have the same error, did you manage to find a solution ?
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.
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! 🙇♂️