Attempt to fix inability to logout from some instances (subdomains) (#1809)

* slight refactor, tweak params

* fix paths

* remove domain

* remove expires

* Use maxAge instead of expires

---------

Co-authored-by: SleeplessOne1917 <abias1122@gmail.com>
This commit is contained in:
Alec Armbruster 2023-07-04 12:52:14 -04:00 committed by GitHub
parent f6cbc896af
commit 7eddc52c13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 16 deletions

View file

@ -26,6 +26,7 @@ export const updateUnreadCountsInterval = 30000;
export const fetchLimit = 20; export const fetchLimit = 20;
export const relTags = "noopener nofollow"; export const relTags = "noopener nofollow";
export const emDash = "\u2014"; export const emDash = "\u2014";
export const authCookieName = "jwt";
/** /**
* Accepted formats: * Accepted formats:

View file

@ -1,7 +1,5 @@
// import Cookies from 'js-cookie';
import { isAuthPath } from "@utils/app"; import { isAuthPath } from "@utils/app";
import { isBrowser } from "@utils/browser"; import { clearAuthCookie, isBrowser, setAuthCookie } from "@utils/browser";
import { isHttps } from "@utils/env";
import * as cookie from "cookie"; import * as cookie from "cookie";
import jwt_decode from "jwt-decode"; import jwt_decode from "jwt-decode";
import { LoginResponse, MyUserInfo } from "lemmy-js-client"; import { LoginResponse, MyUserInfo } from "lemmy-js-client";
@ -31,15 +29,10 @@ export class UserService {
public login(res: LoginResponse) { public login(res: LoginResponse) {
const expires = new Date(); const expires = new Date();
expires.setDate(expires.getDate() + 365); expires.setDate(expires.getDate() + 365);
if (isBrowser() && res.jwt) { if (isBrowser() && res.jwt) {
toast(I18NextService.i18n.t("logged_in")); toast(I18NextService.i18n.t("logged_in"));
document.cookie = cookie.serialize("jwt", res.jwt, { setAuthCookie(res.jwt);
expires,
secure: isHttps(),
domain: location.hostname,
sameSite: true,
path: "/",
});
this.#setJwtInfo(); this.#setJwtInfo();
} }
} }
@ -47,14 +40,11 @@ export class UserService {
public logout() { public logout() {
this.jwtInfo = undefined; this.jwtInfo = undefined;
this.myUserInfo = undefined; this.myUserInfo = undefined;
if (isBrowser()) { if (isBrowser()) {
document.cookie = cookie.serialize("jwt", "", { clearAuthCookie();
maxAge: 0,
path: "/",
domain: location.hostname,
sameSite: true,
});
} }
if (isAuthPath(location.pathname)) { if (isAuthPath(location.pathname)) {
location.replace("/"); location.replace("/");
} else { } else {
@ -64,14 +54,17 @@ export class UserService {
public auth(throwErr = false): string | undefined { public auth(throwErr = false): string | undefined {
const jwt = this.jwtInfo?.jwt; const jwt = this.jwtInfo?.jwt;
if (jwt) { if (jwt) {
return jwt; return jwt;
} else { } else {
const msg = "No JWT cookie found"; const msg = "No JWT cookie found";
if (throwErr && isBrowser()) { if (throwErr && isBrowser()) {
console.error(msg); console.error(msg);
toast(I18NextService.i18n.t("not_logged_in"), "danger"); toast(I18NextService.i18n.t("not_logged_in"), "danger");
} }
return undefined; return undefined;
// throw msg; // throw msg;
} }
@ -80,6 +73,7 @@ export class UserService {
#setJwtInfo() { #setJwtInfo() {
if (isBrowser()) { if (isBrowser()) {
const { jwt } = cookie.parse(document.cookie); const { jwt } = cookie.parse(document.cookie);
if (jwt) { if (jwt) {
this.jwtInfo = { jwt, claims: jwt_decode(jwt) }; this.jwtInfo = { jwt, claims: jwt_decode(jwt) };
} }

View file

@ -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: "/",
});
}

View file

@ -1,19 +1,23 @@
import canShare from "./can-share"; import canShare from "./can-share";
import clearAuthCookie from "./clear-auth-cookie";
import dataBsTheme from "./data-bs-theme"; import dataBsTheme from "./data-bs-theme";
import isBrowser from "./is-browser"; import isBrowser from "./is-browser";
import isDark from "./is-dark"; import isDark from "./is-dark";
import loadCss from "./load-css"; import loadCss from "./load-css";
import restoreScrollPosition from "./restore-scroll-position"; import restoreScrollPosition from "./restore-scroll-position";
import saveScrollPosition from "./save-scroll-position"; import saveScrollPosition from "./save-scroll-position";
import setAuthCookie from "./set-auth-cookie";
import share from "./share"; import share from "./share";
export { export {
canShare, canShare,
clearAuthCookie,
dataBsTheme, dataBsTheme,
isBrowser, isBrowser,
isDark, isDark,
loadCss, loadCss,
restoreScrollPosition, restoreScrollPosition,
saveScrollPosition, saveScrollPosition,
setAuthCookie,
share, share,
}; };

View file

@ -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: "/",
});
}