Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 8 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/shared/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 10 additions & 16 deletions src/shared/services/UserService.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -31,30 +29,22 @@ 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();
}
}

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 {
Expand All @@ -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;
}
Expand All @@ -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) };
}
Expand Down
10 changes: 10 additions & 0 deletions src/shared/utils/browser/clear-auth-cookie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as cookie from "cookie";
import { authCookieName } from "../../config";

export default function clearAuthCookie() {
document.cookie = cookie.serialize(authCookieName, "", {
maxAge: -1,
SleeplessOne1917 marked this conversation as resolved.
Show resolved Hide resolved
sameSite: true,
path: "/",
});
}
4 changes: 4 additions & 0 deletions src/shared/utils/browser/index.ts
Original file line number Diff line number Diff line change
@@ -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,
};
12 changes: 12 additions & 0 deletions src/shared/utils/browser/set-auth-cookie.ts
Original file line number Diff line number Diff line change
@@ -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: "/",
});
}