Angular2-jwt: Not able to import JwtHelperService inside my service.

Created on 2 Feb 2018  ·  4Comments  ·  Source: auth0/angular2-jwt

import { JwtModule, JwtHelperService } from '@auth0/angular-jwt';

then i am trying inside the constructor
constructor(
private http: Http,
private jwtHelperService: JwtHelperService
) { }

After i am getting this error in console

Error: StaticInjectorError(AppModule)[AuthService -> JwtHelperService]:
StaticInjectorError(Platform: core)[AuthService -> JwtHelperService]:
NullInjectorError: No provider for JwtHelperService!

Can you please help how to resolve.

stale

Most helpful comment

I avoided this issue by NOT using constructor assignment.

import { JwtModule, JwtHelperService } from '@auth0/angular-jwt';

export class AuthService {

public jwtHelper: JwtHelperService = new JwtHelperService();

    constructor(
    private http: Http,
    ) { }
         decodeToken() {
             const token = localStorage.getItem('token');
             return this.jwtHelper.decodeToken(token);
         }
}

All 4 comments

That error is saying _"The module injecting your AuthService (AppModule) needs to provide a JwtHelperService to your AuthService."_

If you're trying to use Injection, make sure the module which provides your AuthService (AppModule) also provides a JwtHelperService. See https://github.com/auth0/angular2-jwt#usage-injection.

If you don't want injection, then you'll need to instantiate your helper service manually. See https://github.com/auth0/angular2-jwt#usage-standalone.

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

I avoided this issue by NOT using constructor assignment.

import { JwtModule, JwtHelperService } from '@auth0/angular-jwt';

export class AuthService {

public jwtHelper: JwtHelperService = new JwtHelperService();

    constructor(
    private http: Http,
    ) { }
         decodeToken() {
             const token = localStorage.getItem('token');
             return this.jwtHelper.decodeToken(token);
         }
}

Yes, kittowned it worked for me.

Was this page helpful?
0 / 5 - 0 ratings