2

I am pretty new to angular, I fetched code :

url = https://github.com/DavideViolante/Angular-Full-Stack

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';

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

import { UserService } from './user.service';
import { HttpClient } from '@angular/common/http';
import { User } from '../shared/models/user.model';

import 'rxjs/add/operator/map';

@Injectable()
export class AuthService {
  loggedIn = false;
  isAdmin = false;

  currentUser: User = new User();

  constructor(private userService: UserService,
              private http: HttpClient,
              private router: Router,
              private jwtHelper: JwtHelperService) {
    const token = localStorage.getItem('token');
    if (token) {
      const decodedUser = this.decodeUserFromToken(token);
      this.setCurrentUser(decodedUser);
    }
  }

  login(emailAndPassword) {
    return this.userService.login(emailAndPassword).map(
      res => {
        localStorage.setItem('token', res.token);
        const decodedUser = this.decodeUserFromToken(res.token);
        this.setCurrentUser(decodedUser);
        return this.loggedIn;
      }
    );
  }

  decodeUserFromToken(token) {
    return this.jwtHelper.decodeToken(token).user;
  }
}

I am using Visual studio code and it is not throwing any syntax error.

But getting error in console :

Error: StaticInjectorError(AppModule)[AuthService -> JwtHelperService]: 
  StaticInjectorError(Platform: core)[AuthService -> JwtHelperService]: 
    NullInjectorError: No provider for JwtHelperService!
    at _NullInjector.get (core.js:994)
    at resolveToken (core.js:1292)
    at tryResolveToken (core.js:1234)
    at StaticInjector.get (core.js:1102)
    at resolveToken (core.js:1292)
    at tryResolveToken (core.js:1234)
slavoo
  • 5,798
  • 64
  • 37
  • 39
Anil Kumar
  • 209
  • 1
  • 4
  • 15
  • You import angular-jwt this is for angularjs applications. Install the angular2-jwt instead – ochs.tobi Mar 05 '18 at 11:51
  • getting message while installing above "angular2-jwt@0.2.3 requires a peer of @angular/core@^2.0.0||^4.0.0 but none was installed" – Anil Kumar Mar 05 '18 at 12:05
  • Have you tried: `import { JwtHelperService } from '@auth0/angular-jwt';` after `npm install @auth0/angular-jwt --save` – ValRob Apr 09 '18 at 18:13
  • it fixed now... https://github.com/DavideViolante/Angular-Full-Stack/issues/173 i had closed it – Anil Kumar Apr 11 '18 at 04:30

2 Answers2

0

You have to add AuthGuard to the providers list in app.module:

  providers: [
    AuthGuard
  ],
sridesmet
  • 875
  • 9
  • 19
0

This error is coming just because you have not added jwtmodule in app module file. So here you need to add it with it's configuration so, please add following code into your app.module.ts file:

import { JwtHelperService, JwtModule } from '@auth0/angular-jwt';
...
@NgModule({
imports: [
     ...
    JwtModule.forRoot({
    config: {
      tokenGetter: function  tokenGetter() { 
      return localStorage.getItem('token');
      } 
   }
 });
 ...
 ],
 ...
 })
Kinjal
  • 91
  • 2
  • 9
  • Code-only answers are discouraged. Please click on edit and add some words summarising how your code addresses the question, or perhaps explain how your answer differs from the previous answer/answers. Thanks – Nick Dec 18 '18 at 11:13
  • Now, is it ok? @Nick – Kinjal Dec 19 '18 at 09:10
  • Thanks - you've made it a lot more useful. – Nick Dec 19 '18 at 09:21