Angular2-jwt: AUTH_PROVIDERS

Created on 10 Jan 2017  Â·  8Comments  Â·  Source: auth0/angular2-jwt

Using angular 2, and the angular command line interface.

Per the instructions, in app.module.ts, I imported

import { AUTH_PROVIDERS } from 'angular2-jwt';

and down below, in providers:

providers: [AUTH_PROVIDERS]

But, when i try to serve the application ('ng serve'), I get the following error:

ERROR in Error encountered resolving symbol values statically. Only initialized variables and constants can be referenced because the value of this variable is needed by the template compiler (position 79:22 in the original .ts file), resolving symbol AUTH_PROVIDERS in [MYPROJECT]/node_modules/angular2-jwt/angular2-jwt.d.ts, resolving symbol AppModule in [MYPROJECT]/src/app/app.module.ts, resolving symbol AppModule in [MYPROJECT]/src/app/app.module.ts

"line 79:220 in the original .ts file" seems to point to angular2-jwt.d.ts, and i see that the line is declaring constant AUTH_PROVIDERS: Provider[]). I'm not very good at javascript; is this supposed to be initialized to someting?

thanks in advance

question

Most helpful comment

The readme needs to be updated to remove those instructions. Can you try with a factory function?

export function authHttpServiceFactory(http: Http, options: RequestOptions) {
  return new AuthHttp(new AuthConfig({
    tokenName: 'token',
          tokenGetter: (() => localStorage.getItem('token')),
          globalHeaders: [{'Content-Type':'application/json'}],
     }), http, options);
}

@NgModule({
  // ...

  providers: [
    // ...
    {
      provide: AuthHttp,
      useFactory: authHttpServiceFactory,
      deps: [Http, RequestOptions]
    }
  ]
})

All 8 comments

The readme needs to be updated to remove those instructions. Can you try with a factory function?

export function authHttpServiceFactory(http: Http, options: RequestOptions) {
  return new AuthHttp(new AuthConfig({
    tokenName: 'token',
          tokenGetter: (() => localStorage.getItem('token')),
          globalHeaders: [{'Content-Type':'application/json'}],
     }), http, options);
}

@NgModule({
  // ...

  providers: [
    // ...
    {
      provide: AuthHttp,
      useFactory: authHttpServiceFactory,
      deps: [Http, RequestOptions]
    }
  ]
})

Thank you @chenkie. Your solution works

@chenkie You are a life saver. Exactly the solution I was looking for.

@kartikgreen

 export function authHttpServiceFactory(http: Http, options: RequestOptions) {
  return new AuthHttp(new AuthConfig({
    tokenName: 'token',
          tokenGetter: (() => localStorage.getItem('token')),
          globalHeaders: [{'Content-Type':'application/json'}],
     }), http, options);
}

Following standard coding guidelines this should go in its own file (i.e. authHttpServiceFactory.ts)

You then reference it in app.module.ts and you modify the @NgModule section accordingly.

@kartikgreen Usage of class comes down to personal preference. Some use them, some don't. Experience will let you know when and where to use it.

In NgModule you add the related code snippet in providers.

The snippets purpose is to replace AUTH_PROVIDERS usage.

@dchinn Did this resolve your problem? Please reopen if not.

@chenkie Your solution does'nt works fine for me. When i trying to use a factory function i get the follow error:

Error: Error encountered resolving symbol values statically. Calling function ‘omakeDecorator’, function calls are not supported. Consider replacing the function or lambda with a reference to an exported function

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kolkov picture kolkov  Â·  3Comments

getglad picture getglad  Â·  5Comments

leosvelperez picture leosvelperez  Â·  5Comments

dmitrybz picture dmitrybz  Â·  4Comments

huineng picture huineng  Â·  4Comments