Trying another SSR fix. #2243 (#2251)

This commit is contained in:
Dessalines 2023-11-29 09:44:31 -05:00 committed by GitHub
parent 684662c90b
commit 7222632389
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 18 deletions

View file

@ -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",

View file

@ -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) {

View file

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

View file

@ -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"

View file

@ -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) };
}
}
}