1 import * as Cookies from 'js-cookie';
2 import { User, LoginResponse } from '../interfaces';
3 import { setTheme } from '../utils';
4 import * as jwt_decode from 'jwt-decode';
5 import { Subject } from 'rxjs';
7 export class UserService {
8 private static _instance: UserService;
10 public sub: Subject<{ user: User; unreadCount: number }> = new Subject<{
15 private constructor() {
16 let jwt = Cookies.get('jwt');
21 console.log('No JWT cookie found.');
25 public login(res: LoginResponse) {
26 this.setUser(res.jwt);
27 Cookies.set('jwt', res.jwt, { expires: 365 });
28 console.log('jwt cookie set');
32 this.user = undefined;
33 Cookies.remove('jwt');
35 this.sub.next({ user: undefined, unreadCount: 0 });
36 console.log('Logged out.');
39 public get auth(): string {
40 return Cookies.get('jwt');
43 private setUser(jwt: string) {
44 this.user = jwt_decode(jwt);
45 setTheme(this.user.theme);
46 this.sub.next({ user: this.user, unreadCount: 0 });
47 console.log(this.user);
50 public static get Instance() {
51 return this._instance || (this._instance = new this());