1 import * as Cookies from 'js-cookie';
2 import { User, LoginResponse } from '../interfaces';
3 import * as jwt_decode from 'jwt-decode';
4 import { Subject } from 'rxjs';
6 export class UserService {
7 private static _instance: UserService;
9 public sub: Subject<User> = new Subject<User>();
11 private constructor() {
12 let jwt = Cookies.get("jwt");
16 console.log('No JWT cookie found.');
21 public login(res: LoginResponse) {
22 this.setUser(res.jwt);
23 Cookies.set("jwt", res.jwt);
24 console.log("jwt cookie set");
29 Cookies.remove("jwt");
30 console.log("Logged out.");
34 public get loggedIn(): boolean {
35 return this.user !== undefined;
38 public get auth(): string {
39 return Cookies.get("jwt");
42 private setUser(jwt: string) {
43 this.user = jwt_decode(jwt);
44 this.sub.next(this.user);
45 console.log(this.user);
48 public static get Instance(){
49 return this._instance || (this._instance = new this());