Search code examples
angularangular2-jwt

angular2-jwt - Calling function 'provideAuth', function calls are not supported


I'm using angular2-jwt plugin for Authentication in my angular2/4 app. but after setting up the environment i can use it for once. after rebuilding the project the below error shows up.

error:

ERROR in Error encountered resolving symbol values statically. Calling function 'provideAuth', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol AppModule in /home/user/projects/project/src/app/app.module.ts, resolving symbol AppModule in /home/user/projects/project/src/app/app.module.ts

my app.module.ts

@NgModule({
imports: [
   HttpModule,
   BrowserModule,
   BrowserAnimationsModule,
   AppRoutingModule,
   BsDropdownModule.forRoot(),
   TabsModule.forRoot(),
   ChartsModule,
   LaddaModule,
   FormsModule,
   ToasterModule
],
declarations: [
   AppComponent,
   LoginComponent,
   FullLayoutComponent,
   AsideToggleDirective,
   DashboardMenuComponent,
],
providers: [
  {
  provide: LocationStrategy,
  // useFactory: authHttpServiceFactory,
  // deps: [Http, RequestOptions],
  useClass: HashLocationStrategy,
  },
  AuthenticationService,
  AuthGuard,
  Auth,
  AuthHttp,
  provideAuth({
     headerName: 'Authorization',
     headerPrefix: 'bearer',
     tokenName: 'token',
     // tokenGetter: (() => localStorage.getItem('token')),
     globalHeaders: [{'Content-Type': 'application/json'}],
     noJwtError: true
})
],
bootstrap: [AppComponent]
})

after getting this error i export the function like this:

export function authHttpServiceFactory(http: Http, options: RequestOptions) {
return new AuthHttp(new AuthConfig(), http, options);
}

and imported in providers but the error will be:

ERROR Error: Uncaught (in promise): Error: No provider for AuthHttp!

after adding AuthHttp in providers the error will be:

ERROR Error: Uncaught (in promise): Error: No provider for AuthConfig!


Solution

  • Provide as

        providers: [
         AuthHttp,
         {
            provide: AuthHttp,
            useFactory: authHttpServiceFactory,
            deps: [Http, RequestOptions]
         }
       ]
    
    
       export function authHttpServiceFactory(http: Http, options: RequestOptions) {
           return new AuthHttp(new AuthConfig({
              headerName: 'Authorization',
              headerPrefix: 'bearer',
              tokenName: 'token',
              // tokenGetter: (() => localStorage.getItem('token')),
              globalHeaders: [{'Content-Type': 'application/json'}],
              noJwtError: true
          }), http, options);
        }