X-Git-Url: http://these/git/?a=blobdiff_plain;f=src%2Fshared%2Fservices%2FUserService.ts;h=70e8e9cadae7b79e8de43aa288ce14adf8031b58;hb=c3ab9e74f8775f4b811866d2675b00f9702bde3d;hp=2bb6e16a84c9c2e0c1d545e0319489c63d06412f;hpb=a7592d74bb09d549bace6861413bc34f8c26fd9a;p=lemmy-ui.git diff --git a/src/shared/services/UserService.ts b/src/shared/services/UserService.ts index 2bb6e16..70e8e9c 100644 --- a/src/shared/services/UserService.ts +++ b/src/shared/services/UserService.ts @@ -1,7 +1,5 @@ -// import Cookies from 'js-cookie'; import { isAuthPath } from "@utils/app"; -import { isBrowser } from "@utils/browser"; -import { isHttps } from "@utils/env"; +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"; @@ -28,17 +26,19 @@ export class UserService { this.#setJwtInfo(); } - public login(res: LoginResponse) { + public login({ + res, + showToast = true, + }: { + res: LoginResponse; + showToast?: boolean; + }) { const expires = new Date(); expires.setDate(expires.getDate() + 365); + if (isBrowser() && res.jwt) { - toast(I18NextService.i18n.t("logged_in")); - document.cookie = cookie.serialize("jwt", res.jwt, { - expires, - secure: isHttps(), - domain: location.hostname, - sameSite: true, - }); + showToast && toast(I18NextService.i18n.t("logged_in")); + setAuthCookie(res.jwt); this.#setJwtInfo(); } } @@ -46,14 +46,11 @@ export class UserService { public logout() { this.jwtInfo = undefined; this.myUserInfo = undefined; + if (isBrowser()) { - document.cookie = cookie.serialize("jwt", "", { - maxAge: 0, - path: "/", - domain: location.hostname, - sameSite: true, - }); + clearAuthCookie(); } + if (isAuthPath(location.pathname)) { location.replace("/"); } else { @@ -63,14 +60,17 @@ export class UserService { 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; } @@ -79,6 +79,7 @@ export class UserService { #setJwtInfo() { if (isBrowser()) { const { jwt } = cookie.parse(document.cookie); + if (jwt) { this.jwtInfo = { jwt, claims: jwt_decode(jwt) }; }