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,
// 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();
}
// 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";
--- /dev/null
+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);
+}
+++ /dev/null
-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"));
-}