X-Git-Url: http://these/git/?a=blobdiff_plain;f=src%2Fshared%2Fservices%2FUserService.ts;h=0724f400f3128e267ee869d54c9f3b78cc4145db;hb=7eddc52c1302a1f0d7189576ace44957d6b42d2a;hp=87eca1a201adf6efb68e32106882c5b4fd2a6184;hpb=6a3329f9a2bf75c94bcd73d7845a62ffa4ca9650;p=lemmy-ui.git diff --git a/src/shared/services/UserService.ts b/src/shared/services/UserService.ts index 87eca1a..0724f40 100644 --- a/src/shared/services/UserService.ts +++ b/src/shared/services/UserService.ts @@ -1,59 +1,86 @@ -// import Cookies from 'js-cookie'; -import IsomorphicCookie from 'isomorphic-cookie'; -import { UserSafeSettings, LoginResponse } from 'lemmy-js-client'; -import jwt_decode from 'jwt-decode'; -import { Subject, BehaviorSubject } from 'rxjs'; +import { isAuthPath } from "@utils/app"; +import { clearAuthCookie, isBrowser, setAuthCookie } from "@utils/browser"; +import * as cookie from "cookie"; +import jwt_decode from "jwt-decode"; +import { LoginResponse, MyUserInfo } from "lemmy-js-client"; +import { toast } from "../toast"; +import { I18NextService } from "./I18NextService"; interface Claims { - id: number; + sub: number; iss: string; + iat: number; +} + +interface JwtInfo { + claims: Claims; + jwt: string; } export class UserService { - private static _instance: UserService; - public user: UserSafeSettings; - public claims: Claims; - public jwtSub: Subject = new Subject(); - public unreadCountSub: BehaviorSubject = new BehaviorSubject( - 0 - ); + static #instance: UserService; + public myUserInfo?: MyUserInfo; + public jwtInfo?: JwtInfo; private constructor() { - if (this.auth) { - this.setClaims(this.auth); - } else { - // setTheme(); - console.log('No JWT cookie found.'); - } + this.#setJwtInfo(); } public login(res: LoginResponse) { - let expires = new Date(); + const expires = new Date(); expires.setDate(expires.getDate() + 365); - IsomorphicCookie.save('jwt', res.jwt, { expires, secure: false }); - console.log('jwt cookie set'); - this.setClaims(res.jwt); + + if (isBrowser() && res.jwt) { + toast(I18NextService.i18n.t("logged_in")); + setAuthCookie(res.jwt); + this.#setJwtInfo(); + } } public logout() { - IsomorphicCookie.remove('jwt', { secure: false }); - this.claims = undefined; - this.user = undefined; - // setTheme(); - this.jwtSub.next(); - console.log('Logged out.'); + this.jwtInfo = undefined; + this.myUserInfo = undefined; + + if (isBrowser()) { + clearAuthCookie(); + } + + if (isAuthPath(location.pathname)) { + location.replace("/"); + } else { + location.reload(); + } } - public get auth(): string { - return IsomorphicCookie.load('jwt'); + public auth(throwErr = false): string | undefined { + const jwt = this.jwtInfo?.jwt; + + if (jwt) { + return jwt; + } else { + const msg = "No JWT cookie found"; + + if (throwErr && isBrowser()) { + console.error(msg); + toast(I18NextService.i18n.t("not_logged_in"), "danger"); + } + + return undefined; + // throw msg; + } } - private setClaims(jwt: string) { - this.claims = jwt_decode(jwt); - this.jwtSub.next(jwt); + #setJwtInfo() { + if (isBrowser()) { + const { jwt } = cookie.parse(document.cookie); + + if (jwt) { + this.jwtInfo = { jwt, claims: jwt_decode(jwt) }; + } + } } public static get Instance() { - return this._instance || (this._instance = new this()); + return this.#instance || (this.#instance = new this()); } }