107. Check for logged in user (#12)
This commit is contained in:
@ -4,6 +4,7 @@ import {HttpClient, HttpErrorResponse, HttpResponse} from "@angular/common/http"
|
||||
import {UserLogin} from "../dto/user-login";
|
||||
import {Observable} from "rxjs";
|
||||
import {User} from "../model/user";
|
||||
import {JwtHelperService} from "@auth0/angular-jwt";
|
||||
|
||||
const USER_STORAGE_KEY = "user";
|
||||
const JWT_TOKEN_STORAGE_KEY = "jwt-token";
|
||||
@ -18,6 +19,9 @@ export class AuthenticationService {
|
||||
private loggedInUser: string | null;
|
||||
private storage = localStorage;
|
||||
|
||||
//first install this module: `npm install @auth0/angular-jwt`
|
||||
private jwtHelper: JwtHelperService = new JwtHelperService();
|
||||
|
||||
constructor(private httpClient: HttpClient) {
|
||||
}
|
||||
|
||||
@ -61,4 +65,19 @@ export class AuthenticationService {
|
||||
return this.token;
|
||||
}
|
||||
|
||||
public isLoggedIn(): boolean {
|
||||
this.loadToken();
|
||||
if (this.token != null && this.token !== '') {
|
||||
let subject = this.jwtHelper.decodeToken(this.token).sub;
|
||||
if (subject != null || '') {
|
||||
if (!this.jwtHelper.isTokenExpired(this.token!)) {
|
||||
this.loggedInUser = subject;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.logout()
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user