user2129896
user2129896

Reputation: 307

Module "node_modules/@auth0/angular-jwt/index"' has no exported member 'JwtHelper'

in the AuthService class I can not import:

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

Error:

  Module '"eclipse:angular-login-hide-navbar-
  ngif4/node_modules/@auth0/angular-jwt/index"' has no exported member 'JwtHelper'.

I made the command:

   npm install @auth0/angular-jwt --save

I have catalogs:

 - node_modules
  -- @auth0
   --- angular-jwt

Upvotes: 0

Views: 2665

Answers (2)

user2129896
user2129896

Reputation: 307

it works:

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

but login is not working correctly. The user logged in, after refreshing the page user is logged out .

How to do this that logged in user you do not log out? When it refreshes the page.

auth/auth.service.ts

    import {Injectable} from '@angular/core';
    import {Router} from '@angular/router';
    import {BehaviorSubject} from 'rxjs/BehaviorSubject';
    import {User} from './user';
    import {Http, Headers, RequestOptions, Response} from '@angular/http';
    import 'rxjs/add/operator/map';
    import 'rxjs/add/operator/toPromise';
    import {AppComponent} from '../app.component';
    import {Observable} from 'rxjs/';
    import {JwtHelperService} from '@auth0/angular-jwt';



    @Injectable()

    export class AuthService {

      private loggedIn = new BehaviorSubject<boolean>(false);
      private loggedUser = new BehaviorSubject<User>(<User>{});
      private loggedPassword = new BehaviorSubject<User>(<User>{});
      result: any;

      _loggedIn: boolean;


      get isLoggedIn() {
        return this.loggedIn.asObservable();
      }

      constructor(private router: Router, private _http: Http, public jwtHelperService: JwtHelperService) {

      }


        login(user: User) {
            this.getUsers().subscribe(data => {

                for (const it of data) {

                    if (user.userName === it.name && user.password === it.password) {


                        this._loggedIn = true;
                        this.loggedIn.next(true);
                        this.loggedUser.next(user);
                        this.loggedPassword.next(user);
                        this.router.navigate(['/']);          


                        this.loggedIn_test();

                    }
                }
            });
       }



        getUsers() {  

            return this._http.get('/api/users').map(result => this.result = result.json().data);
        }

        loggedIn_test() {
            const token: string = this.jwtHelperService.tokenGetter();

            if (!token) {
              return false;
            }

            const tokenExpired: boolean = this.jwtHelperService.isTokenExpired(token);

            return !tokenExpired;


        }

      /*

        logout() {
          this.loggedIn.next(false);
          this.router.navigate(['/login']);
        }
      */
    }

auth/auth.guard.ts:

import {Injectable} from '@angular/core';
import {
  CanActivate,
  ActivatedRouteSnapshot,
  RouterStateSnapshot,
  Router
} from '@angular/router';
import {AuthService} from './auth.service';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/take';
import 'rxjs/add/operator/map';


@Injectable()

export class AuthGuard implements CanActivate {
  constructor(
    private authService: AuthService,
    private router: Router
  ) {}

  canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
    return this.authService.isLoggedIn
      .take(1)
      .map((isLoggedIn: boolean) => {
        if (!isLoggedIn && !this.authService.loggedIn_test()) {  
          localStorage.removeItem('token');
          this.router.navigate(['/login']);
          return false;
        }
        return true;
      });
  }
}

Upvotes: 1

Mike Adamenko
Mike Adamenko

Reputation: 3002

Looks like it's open issue in the angular2-jwt. Look at here

Upvotes: 0

Related Questions