]> Untitled Git - lemmy-ui.git/commitdiff
Attempt to fix inability to logout from some instances (subdomains) (#1809)
authorAlec Armbruster <35377827+alectrocute@users.noreply.github.com>
Tue, 4 Jul 2023 16:52:14 +0000 (12:52 -0400)
committerGitHub <noreply@github.com>
Tue, 4 Jul 2023 16:52:14 +0000 (12:52 -0400)
* slight refactor, tweak params

* fix paths

* remove domain

* remove expires

* Use maxAge instead of expires

---------

Co-authored-by: SleeplessOne1917 <abias1122@gmail.com>
src/shared/config.ts
src/shared/services/UserService.ts
src/shared/utils/browser/clear-auth-cookie.ts [new file with mode: 0644]
src/shared/utils/browser/index.ts
src/shared/utils/browser/set-auth-cookie.ts [new file with mode: 0644]

index 58ecc08b19ad1b48d1601ff52c5145c861f758b1..38c521ecebd2772e968457419d55c90c85e36715 100644 (file)
@@ -26,6 +26,7 @@ export const updateUnreadCountsInterval = 30000;
 export const fetchLimit = 20;
 export const relTags = "noopener nofollow";
 export const emDash = "\u2014";
+export const authCookieName = "jwt";
 
 /**
  * Accepted formats:
index 3757e2369604da65cb19c9cbe2a3b3f087059cdf..0724f400f3128e267ee869d54c9f3b78cc4145db 100644 (file)
@@ -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";
@@ -31,15 +29,10 @@ export class UserService {
   public login(res: LoginResponse) {
     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,
-        path: "/",
-      });
+      setAuthCookie(res.jwt);
       this.#setJwtInfo();
     }
   }
@@ -47,14 +40,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 {
@@ -64,14 +54,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;
     }
@@ -80,6 +73,7 @@ export class UserService {
   #setJwtInfo() {
     if (isBrowser()) {
       const { jwt } = cookie.parse(document.cookie);
+
       if (jwt) {
         this.jwtInfo = { jwt, claims: jwt_decode(jwt) };
       }
diff --git a/src/shared/utils/browser/clear-auth-cookie.ts b/src/shared/utils/browser/clear-auth-cookie.ts
new file mode 100644 (file)
index 0000000..f5cc73f
--- /dev/null
@@ -0,0 +1,10 @@
+import * as cookie from "cookie";
+import { authCookieName } from "../../config";
+
+export default function clearAuthCookie() {
+  document.cookie = cookie.serialize(authCookieName, "", {
+    maxAge: -1,
+    sameSite: true,
+    path: "/",
+  });
+}
index d07b16e7c8a9b2f0abdbdc933ddf3d768c84a525..321a4c9f46b7ee18637f855e349f77c76234f8a8 100644 (file)
@@ -1,19 +1,23 @@
 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";
 import loadCss from "./load-css";
 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,
   loadCss,
   restoreScrollPosition,
   saveScrollPosition,
+  setAuthCookie,
   share,
 };
diff --git a/src/shared/utils/browser/set-auth-cookie.ts b/src/shared/utils/browser/set-auth-cookie.ts
new file mode 100644 (file)
index 0000000..e7d4300
--- /dev/null
@@ -0,0 +1,12 @@
+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: "/",
+  });
+}