104. Get user from local cache (#12)
This commit is contained in:
@ -5,13 +5,14 @@ import {UserLogin} from "../dto/user-login";
|
|||||||
import {Observable} from "rxjs";
|
import {Observable} from "rxjs";
|
||||||
import {User} from "../model/user";
|
import {User} from "../model/user";
|
||||||
|
|
||||||
|
const USER_STORAGE_KEY = "user";
|
||||||
|
const JWT_TOKEN_STORAGE_KEY = "jwt-token";
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
export class AuthenticationService {
|
export class AuthenticationService {
|
||||||
|
|
||||||
private readonly JWT_TOKEN_STORAGE_KEY = "jwt-token";
|
|
||||||
|
|
||||||
private host: string = environment.apiUrl;
|
private host: string = environment.apiUrl;
|
||||||
private token: string | null;
|
private token: string | null;
|
||||||
private loggedInUser: string | null;
|
private loggedInUser: string | null;
|
||||||
@ -33,17 +34,23 @@ export class AuthenticationService {
|
|||||||
public logout(): void {
|
public logout(): void {
|
||||||
this.token = null;
|
this.token = null;
|
||||||
this.loggedInUser = null;
|
this.loggedInUser = null;
|
||||||
this.storage.removeItem(this.JWT_TOKEN_STORAGE_KEY);
|
this.storage.removeItem(JWT_TOKEN_STORAGE_KEY);
|
||||||
this.storage.removeItem("user");
|
this.storage.removeItem(USER_STORAGE_KEY);
|
||||||
this.storage.removeItem("users");
|
this.storage.removeItem("users");
|
||||||
}
|
}
|
||||||
|
|
||||||
public saveToken(token: string): void {
|
public saveToken(token: string): void {
|
||||||
this.token = token;
|
this.token = token;
|
||||||
this.storage.setItem(this.JWT_TOKEN_STORAGE_KEY, token);
|
this.storage.setItem(JWT_TOKEN_STORAGE_KEY, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public addUserToLocalStorage(user: User) {
|
public addUserToLocalStorage(user: User) {
|
||||||
this.storage.setItem("user", JSON.stringify(user));
|
this.storage.setItem(USER_STORAGE_KEY, JSON.stringify(user));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getUserFromLocalStorage(): User {
|
||||||
|
let userJson = this.storage.getItem(USER_STORAGE_KEY);
|
||||||
|
return JSON.parse(userJson!);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user