Trying another SSR fix. #2243

This commit is contained in:
Dessalines 2023-11-29 09:36:59 -05:00
parent 684662c90b
commit 9e67c1bb65
5 changed files with 23 additions and 18 deletions

View file

@ -70,7 +70,7 @@
"inferno-router": "^8.2.2", "inferno-router": "^8.2.2",
"inferno-server": "^8.2.2", "inferno-server": "^8.2.2",
"jwt-decode": "^4.0.0", "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", "lodash.isequal": "^4.5.0",
"markdown-it": "^13.0.1", "markdown-it": "^13.0.1",
"markdown-it-bidi": "^0.1.0", "markdown-it-bidi": "^0.1.0",

View file

@ -20,6 +20,7 @@ import {
import { createSsrHtml } from "../utils/create-ssr-html"; import { createSsrHtml } from "../utils/create-ssr-html";
import { getErrorPageData } from "../utils/get-error-page-data"; import { getErrorPageData } from "../utils/get-error-page-data";
import { setForwardedHeaders } from "../utils/set-forwarded-headers"; import { setForwardedHeaders } from "../utils/set-forwarded-headers";
import { authCookieName } from "../../shared/config";
export default async (req: Request, res: Response) => { export default async (req: Request, res: Response) => {
try { try {
@ -32,7 +33,7 @@ export default async (req: Request, res: Response) => {
); );
const auth = req.headers.cookie const auth = req.headers.cookie
? cookie.parse(req.headers.cookie).jwt ? cookie.parse(req.headers.cookie)[authCookieName]
: undefined; : undefined;
if (auth) { if (auth) {

View file

@ -1,6 +1,9 @@
import * as cookie from "cookie"; import * as cookie from "cookie";
import type { Request } from "express"; import type { Request } from "express";
import { authCookieName } from "../../shared/config";
export function hasJwtCookie(req: Request): boolean { export function hasJwtCookie(req: Request): boolean {
return Boolean(cookie.parse(req.headers.cookie ?? "").jwt?.length); return Boolean(
cookie.parse(req.headers.cookie ?? "")[authCookieName]?.length,
);
} }

View file

@ -26,7 +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"; export const authCookieName = "auth";
// No. of max displayed communities per // No. of max displayed communities per
// page on route "/communities" // page on route "/communities"

View file

@ -7,6 +7,7 @@ import { toast } from "../toast";
import { I18NextService } from "./I18NextService"; import { I18NextService } from "./I18NextService";
import { amAdmin } from "@utils/roles"; import { amAdmin } from "@utils/roles";
import { HttpService } from "."; import { HttpService } from ".";
import { authCookieName } from "../config";
interface Claims { interface Claims {
sub: number; sub: number;
@ -14,18 +15,18 @@ interface Claims {
iat: number; iat: number;
} }
interface JwtInfo { interface AuthInfo {
claims: Claims; claims: Claims;
jwt: string; auth: string;
} }
export class UserService { export class UserService {
static #instance: UserService; static #instance: UserService;
public myUserInfo?: MyUserInfo; public myUserInfo?: MyUserInfo;
public jwtInfo?: JwtInfo; public authInfo?: AuthInfo;
private constructor() { private constructor() {
this.#setJwtInfo(); this.#setAuthInfo();
} }
public login({ public login({
@ -38,12 +39,12 @@ export class UserService {
if (isBrowser() && res.jwt) { if (isBrowser() && res.jwt) {
showToast && toast(I18NextService.i18n.t("logged_in")); showToast && toast(I18NextService.i18n.t("logged_in"));
setAuthCookie(res.jwt); setAuthCookie(res.jwt);
this.#setJwtInfo(); this.#setAuthInfo();
} }
} }
public logout() { public logout() {
this.jwtInfo = undefined; this.authInfo = undefined;
this.myUserInfo = undefined; this.myUserInfo = undefined;
if (isBrowser()) { if (isBrowser()) {
@ -60,10 +61,10 @@ export class UserService {
} }
public auth(throwErr = false): string | undefined { public auth(throwErr = false): string | undefined {
const jwt = this.jwtInfo?.jwt; const auth = this.authInfo?.auth;
if (jwt) { if (auth) {
return jwt; return auth;
} else { } else {
const msg = "No JWT cookie found"; const msg = "No JWT cookie found";
@ -77,13 +78,13 @@ export class UserService {
} }
} }
#setJwtInfo() { #setAuthInfo() {
if (isBrowser()) { if (isBrowser()) {
const { jwt } = cookie.parse(document.cookie); const auth = cookie.parse(document.cookie)[authCookieName];
if (jwt) { if (auth) {
HttpService.client.setHeaders({ Authorization: `Bearer ${jwt}` }); HttpService.client.setHeaders({ Authorization: `Bearer ${auth}` });
this.jwtInfo = { jwt, claims: jwtDecode(jwt) }; this.authInfo = { auth, claims: jwtDecode(auth) };
} }
} }
} }