1 // import Cookies from 'js-cookie';
2 import IsomorphicCookie from 'isomorphic-cookie';
3 import { User, LoginResponse } from 'lemmy-js-client';
4 import { setTheme } from '../utils';
5 import jwt_decode from 'jwt-decode';
6 import { Subject, BehaviorSubject } from 'rxjs';
13 export class UserService {
14 private static _instance: UserService;
16 public claims: Claims;
17 public jwtSub: Subject<string> = new Subject<string>();
18 public unreadCountSub: BehaviorSubject<number> = new BehaviorSubject<number>(
22 private constructor() {
23 let jwt = IsomorphicCookie.load('jwt');
28 console.log('No JWT cookie found.');
32 public login(res: LoginResponse) {
33 this.setClaims(res.jwt);
34 IsomorphicCookie.save('jwt', res.jwt, { expires: 365 });
35 console.log('jwt cookie set');
39 this.claims = undefined;
40 this.user = undefined;
41 IsomorphicCookie.remove('jwt');
44 console.log('Logged out.');
47 public get auth(): string {
48 return IsomorphicCookie.load('jwt');
51 private setClaims(jwt: string) {
52 this.claims = jwt_decode(jwt);
53 this.jwtSub.next(jwt);
56 public static get Instance() {
57 return this._instance || (this._instance = new this());