I am trying to use tokenNotExpired of angular2-jwt for check if user is logged in or not. But when i implement this I get below error in cli:-
ERROR in node_modules/angular2-jwt/angular2-jwt.d.ts(3,10): error TS2305: Module '"d:/Visual Studio/asp.net/mean_blog/client/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'.
I have install angular2-jwt using "npm install angular2-jwt@latest --save" commmand.
Below is the auth.service.ts code:-
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import { map, take } from 'rxjs/operators';
import { tokenNotExpired } from 'angular2-jwt';
@Injectable({
providedIn: 'root'
})
export class AuthService {
authToken;
user;
options;
constructor(private http: Http) { }
createAuthenticationHeaders(){
this.loadToken();
this.options = new RequestOptions({
headers:new Headers({
'Content-Type':'application/json',
'authorization':this.authToken
})
})
}
loadToken(){
this.authToken = localStorage.getItem('token');
}
registerUser(user) {
return this.http.post('/api/auth/register', user).pipe(map(res => res.json()));
}
checkUsername(username) {
return this.http.get('/api/auth/checkUsername/' + username).pipe(map(res => res.json()));
}
checkEMail(email) {
return this.http.get('/api/auth/checkEmail/' + email).pipe(map(res => res.json()));
}
login(user){
return this.http.post('/api/auth/login',user).pipe(map(res=>res.json()));
}
logout(){
this.authToken = null;
this.user = null;
localStorage.clear();
}
storeUserData(token, user){
localStorage.setItem('token',token);
localStorage.setItem('user',JSON.stringify(user));
this.authToken = token;
this.user = user
}
getProfile(){
this.createAuthenticationHeaders();
return this.http.get('/api/auth/profile',this.options).pipe(map(res=>res.json()));
}
loggedIn() {
return tokenNotExpired();
}
}
I have also check the dependency in package.json that is also added correctly. I don't know from where this error is occured.
Some function of rxjs was deprecated in angular 6, so I use rxjs-compat for backward compatibility. It solve the issue.