From 72226323895d4f42b9d277f9f5d6aaf7c17276b4 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 29 Nov 2023 09:44:31 -0500 Subject: [PATCH] Trying another SSR fix. #2243 (#2251) --- package.json | 2 +- src/server/handlers/catch-all-handler.tsx | 3 ++- src/server/utils/has-jwt-cookie.ts | 5 +++- src/shared/config.ts | 2 +- src/shared/services/UserService.ts | 29 ++++++++++++----------- 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 0b10df44..450b57fc 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "inferno-router": "^8.2.2", "inferno-server": "^8.2.2", "jwt-decode": "^4.0.0", - "lemmy-js-client": "0.19.0-rc.15", + "lemmy-js-client": "0.19.0-rc.16", "lodash.isequal": "^4.5.0", "markdown-it": "^13.0.1", "markdown-it-bidi": "^0.1.0", diff --git a/src/server/handlers/catch-all-handler.tsx b/src/server/handlers/catch-all-handler.tsx index f4fd8a45..275a4463 100644 --- a/src/server/handlers/catch-all-handler.tsx +++ b/src/server/handlers/catch-all-handler.tsx @@ -20,6 +20,7 @@ import { import { createSsrHtml } from "../utils/create-ssr-html"; import { getErrorPageData } from "../utils/get-error-page-data"; import { setForwardedHeaders } from "../utils/set-forwarded-headers"; +import { authCookieName } from "../../shared/config"; export default async (req: Request, res: Response) => { try { @@ -32,7 +33,7 @@ export default async (req: Request, res: Response) => { ); const auth = req.headers.cookie - ? cookie.parse(req.headers.cookie).jwt + ? cookie.parse(req.headers.cookie)[authCookieName] : undefined; if (auth) { diff --git a/src/server/utils/has-jwt-cookie.ts b/src/server/utils/has-jwt-cookie.ts index ea558ffa..c854e0df 100644 --- a/src/server/utils/has-jwt-cookie.ts +++ b/src/server/utils/has-jwt-cookie.ts @@ -1,6 +1,9 @@ import * as cookie from "cookie"; import type { Request } from "express"; +import { authCookieName } from "../../shared/config"; export function hasJwtCookie(req: Request): boolean { - return Boolean(cookie.parse(req.headers.cookie ?? "").jwt?.length); + return Boolean( + cookie.parse(req.headers.cookie ?? "")[authCookieName]?.length, + ); } 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 64292478..856fca8a 100644 --- a/src/shared/services/UserService.ts +++ b/src/shared/services/UserService.ts @@ -7,6 +7,7 @@ import { toast } from "../toast"; import { I18NextService } from "./I18NextService"; import { amAdmin } from "@utils/roles"; import { HttpService } from "."; +import { authCookieName } from "../config"; interface Claims { sub: number; @@ -14,18 +15,18 @@ interface Claims { iat: number; } -interface JwtInfo { +interface AuthInfo { claims: Claims; - jwt: string; + auth: string; } export class UserService { static #instance: UserService; public myUserInfo?: MyUserInfo; - public jwtInfo?: JwtInfo; + public authInfo?: AuthInfo; private constructor() { - this.#setJwtInfo(); + this.#setAuthInfo(); } public login({ @@ -38,12 +39,12 @@ export class UserService { if (isBrowser() && res.jwt) { showToast && toast(I18NextService.i18n.t("logged_in")); setAuthCookie(res.jwt); - this.#setJwtInfo(); + this.#setAuthInfo(); } } public logout() { - this.jwtInfo = undefined; + this.authInfo = undefined; this.myUserInfo = undefined; if (isBrowser()) { @@ -60,10 +61,10 @@ export class UserService { } public auth(throwErr = false): string | undefined { - const jwt = this.jwtInfo?.jwt; + const auth = this.authInfo?.auth; - if (jwt) { - return jwt; + if (auth) { + return auth; } else { const msg = "No JWT cookie found"; @@ -77,13 +78,13 @@ export class UserService { } } - #setJwtInfo() { + #setAuthInfo() { if (isBrowser()) { - const { jwt } = cookie.parse(document.cookie); + const auth = cookie.parse(document.cookie)[authCookieName]; - if (jwt) { - HttpService.client.setHeaders({ Authorization: `Bearer ${jwt}` }); - this.jwtInfo = { jwt, claims: jwtDecode(jwt) }; + if (auth) { + HttpService.client.setHeaders({ Authorization: `Bearer ${auth}` }); + this.authInfo = { auth, claims: jwtDecode(auth) }; } } }