]> Untitled Git - lemmy-ui.git/commitdiff
wip
authorAlec Armbruster <35377827+alectrocute@users.noreply.github.com>
Fri, 30 Jun 2023 14:04:01 +0000 (10:04 -0400)
committerAlec Armbruster <35377827+alectrocute@users.noreply.github.com>
Fri, 30 Jun 2023 14:04:01 +0000 (10:04 -0400)
src/server/middleware.ts
src/server/utils/has-jwt-cookie.ts [new file with mode: 0644]
src/server/utils/is-request-authenticated.ts [deleted file]

index 7505d6500afccc3c403444c6b1e567c05a70bc06..24ae1b95f42caf6857d786f898422a9e47dab9b9 100644 (file)
@@ -1,5 +1,5 @@
 import type { NextFunction, Request, Response } from "express";
-import { isRequestAuthenticated } from "./utils/is-request-authenticated";
+import { hasJwtCookie } from "./utils/has-jwt-cookie";
 
 export function setDefaultCsp({
   res,
@@ -22,18 +22,13 @@ export function setDefaultCsp({
 // interval is rather arbitrary and could be set higher (less server load) or lower (fresher data).
 //
 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
-export function setCacheControl({
-  res,
-  req,
-  next,
-}: {
-  res: Response;
-  req: Request;
-  next: NextFunction;
-}) {
+export function setCacheControl(
+  req: Request,
+  res: Response,
+  next: NextFunction
+) {
   let caching: string;
 
-  // Avoid any sort of caching in development
   if (process.env.NODE_ENV !== "production") {
     return next();
   }
@@ -45,7 +40,7 @@ export function setCacheControl({
     // Static content gets cached publicly for a day
     caching = "public, max-age=86400";
   } else {
-    if (isRequestAuthenticated(req)) {
+    if (hasJwtCookie(req)) {
       caching = "private";
     } else {
       caching = "public, max-age=5";
diff --git a/src/server/utils/has-jwt-cookie.ts b/src/server/utils/has-jwt-cookie.ts
new file mode 100644 (file)
index 0000000..ea558ff
--- /dev/null
@@ -0,0 +1,6 @@
+import * as cookie from "cookie";
+import type { Request } from "express";
+
+export function hasJwtCookie(req: Request): boolean {
+  return Boolean(cookie.parse(req.headers.cookie ?? "").jwt?.length);
+}
diff --git a/src/server/utils/is-request-authenticated.ts b/src/server/utils/is-request-authenticated.ts
deleted file mode 100644 (file)
index 7b9fb22..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-import type { Request } from "express";
-
-export function isRequestAuthenticated(req: Request): boolean {
-  if (!req.headers.cookie) {
-    return false;
-  }
-
-  return req.headers.cookie?.split("; ").some(c => c.startsWith("jwt"));
-}