2
import { tokenNotExpired } from 'angular2-jwt';

Error:

ERROR in node_modules/angular2-jwt/angular2-jwt.d.ts(3,10): error TS2305: Module '"C:/Users/Charles Edwin Ison/OOP/node_modules/rxjs/Observable"' has no exported member 'Observable'. node_modules/rxjs/Observable.d.ts(1,15): error TS2307: Cannot find module 'rxjs-compat/Observable'.

How to fix this?

coDe murDerer
  • 1,858
  • 4
  • 20
  • 28
C.E James
  • 315
  • 1
  • 5
  • 22
  • Please have a look at the link https://stackoverflow.com/questions/50188560/angular-js-2-node-modules-rxjs-observable-has-no-exported-member-observable – Ankur Shah Oct 08 '18 at 10:09
  • Possible duplicate of [I get an error when learning Angular."has no exported member 'Observable'"](https://stackoverflow.com/questions/49840152/i-get-an-error-when-learning-angular-has-no-exported-member-observable) – coDe murDerer Oct 08 '18 at 10:15

4 Answers4

0

I resolved this issue using @auth0/angular-jwt with angular 6. You first need to uninstall angular2-jwt completely:

npm uninstall angular2-jwt --save

Then you can run:

npm install @auth0/angular-jwt

Only these two command lines helped me.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Antajo Paulson
  • 555
  • 1
  • 4
  • 15
  • Thanks for sharing your solution. tokenNotExpired isn't available in this library though. How do you import this function? – Reza Taba Sep 10 '20 at 18:13
0

If you re using Angular 4.3 or above you don't have support with old version of angular2-jwt. if you use older version you will get more errors since it relies on an Http Interceptor from Angular's HttpClient.what you can do is download the new version @auth0/angular-jwt and implement method for tokenNotExpired

# installation with npm
npm install @auth0/angular-jwt

simply you can implement in this way

export function tokenGetter(access_token : string) {
  return localStorage.getItem(access_token);
}

private tokenNotExpired(token : string)
{
  const item: string = tokenGetter(token);
  return item != null && !this.jwtHelper.isTokenExpired(item);
}
0
npm uninstall angular2-jwt
npm install @auth0/angular-jwt
npm install rxjs-compat --save

In auth.service.ts:

import { JwtHelperService } from "@auth0/angular-jwt";
 token: string

  isAuthenticated() {
return this.jwtHelper.isTokenExpired(this.token);

}

in header.component.html:

 <a class="nav-link" [routerLink]="['/dashboard']" routerLinkActive="active-link"
       *ngIf="!authService.isAuthenticated()"
       [routerLinkActiveOptions]="{exact: true}">Dashboard</a>
    <a class="btn btn-outline-primary me-2"  *ngIf="authService.isAuthenticated()"
       [routerLink]="['/auth']" tabindex="-1" aria-disabled="true">Sign in</a>
Tatyana Molchanova
  • 1,305
  • 5
  • 12
  • 23
0
  1. Remove the angular2-jwt package
  2. Add the reference to the new package
npm uninstall angular2-jwt
npm install @auth0/angular-jwt

Add the tokenNotExpired function in your code;

import { JwtHelperService } from "@auth0/angular-jwt";
private tokenNotExpired()
{
    const jwtService: JwtHelperService = new JwtHelperService();
    const item: string = jwtService.tokenGetter();
    return item != null && !jwtService.isTokenExpired(item);
}

You can use it wherever you want:

public loggedIn() {
    if (localStorage.getItem('id_token') === '' ||
        localStorage.getItem('id_token') === null ||
        localStorage.getItem('id_token') === undefined) {
      return false;
    } else {
      return this.tokenNotExpired();
    }
}