From 442766c1d813950d178dceda317466bc3c581bee Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 31 Oct 2023 12:42:10 +0100 Subject: [PATCH] Use `auth` cookie set by backend instead of `jwt` (fixes #2193) Requires https://github.com/LemmyNet/lemmy-js-client/pull/208 --- src/shared/config.ts | 2 +- src/shared/services/UserService.ts | 7 ++++--- src/shared/utils/browser/clear-auth-cookie.ts | 10 ---------- src/shared/utils/browser/index.ts | 4 ---- src/shared/utils/browser/set-auth-cookie.ts | 12 ------------ 5 files changed, 5 insertions(+), 30 deletions(-) delete mode 100644 src/shared/utils/browser/clear-auth-cookie.ts delete mode 100644 src/shared/utils/browser/set-auth-cookie.ts diff --git a/src/shared/config.ts b/src/shared/config.ts index 70019742..6718de0a 100644 --- a/src/shared/config.ts +++ b/src/shared/config.ts @@ -26,7 +26,7 @@ export const updateUnreadCountsInterval = 30000; export const fetchLimit = 20; export const relTags = "noopener nofollow"; export const emDash = "\u2014"; -export const authCookieName = "jwt"; +export const authCookieName = "auth"; // No. of max displayed communities per // page on route "/communities" diff --git a/src/shared/services/UserService.ts b/src/shared/services/UserService.ts index 3e459a1a..e5d1afa8 100644 --- a/src/shared/services/UserService.ts +++ b/src/shared/services/UserService.ts @@ -1,5 +1,5 @@ import { isAuthPath } from "@utils/app"; -import { clearAuthCookie, isBrowser, setAuthCookie } from "@utils/browser"; +import { isBrowser } from "@utils/browser"; import * as cookie from "cookie"; import jwt_decode from "jwt-decode"; import { LoginResponse, MyUserInfo } from "lemmy-js-client"; @@ -40,7 +40,6 @@ export class UserService { if (isBrowser() && res.jwt) { showToast && toast(I18NextService.i18n.t("logged_in")); - setAuthCookie(res.jwt); this.#setJwtInfo(); } } @@ -50,7 +49,9 @@ export class UserService { this.myUserInfo = undefined; if (isBrowser()) { - clearAuthCookie(); + // TODO: call logout here + // https://github.com/LemmyNet/lemmy-js-client/pull/208 + //HttpService.client.logout() } if (isAuthPath(location.pathname)) { diff --git a/src/shared/utils/browser/clear-auth-cookie.ts b/src/shared/utils/browser/clear-auth-cookie.ts deleted file mode 100644 index f5cc73f1..00000000 --- a/src/shared/utils/browser/clear-auth-cookie.ts +++ /dev/null @@ -1,10 +0,0 @@ -import * as cookie from "cookie"; -import { authCookieName } from "../../config"; - -export default function clearAuthCookie() { - document.cookie = cookie.serialize(authCookieName, "", { - maxAge: -1, - sameSite: true, - path: "/", - }); -} diff --git a/src/shared/utils/browser/index.ts b/src/shared/utils/browser/index.ts index 41701207..a35a81fe 100644 --- a/src/shared/utils/browser/index.ts +++ b/src/shared/utils/browser/index.ts @@ -1,5 +1,4 @@ import canShare from "./can-share"; -import clearAuthCookie from "./clear-auth-cookie"; import dataBsTheme from "./data-bs-theme"; import isBrowser from "./is-browser"; import isDark from "./is-dark"; @@ -7,12 +6,10 @@ import loadCss from "./load-css"; import platform from "./platform"; import restoreScrollPosition from "./restore-scroll-position"; import saveScrollPosition from "./save-scroll-position"; -import setAuthCookie from "./set-auth-cookie"; import share from "./share"; export { canShare, - clearAuthCookie, dataBsTheme, isBrowser, isDark, @@ -20,6 +17,5 @@ export { platform, restoreScrollPosition, saveScrollPosition, - setAuthCookie, share, }; diff --git a/src/shared/utils/browser/set-auth-cookie.ts b/src/shared/utils/browser/set-auth-cookie.ts deleted file mode 100644 index e7d4300c..00000000 --- a/src/shared/utils/browser/set-auth-cookie.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { isHttps } from "@utils/env"; -import * as cookie from "cookie"; -import { authCookieName } from "../../config"; - -export default function setAuthCookie(jwt: string) { - document.cookie = cookie.serialize(authCookieName, jwt, { - maxAge: 365 * 24 * 60 * 60 * 1000, - secure: isHttps(), - sameSite: true, - path: "/", - }); -}