diff options
author | Raphael <mail@raphaelkabo.com> | 2024-02-26 15:06:01 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-26 15:06:01 +0000 |
commit | de688444d167fdb80c6e88b8ba837405ba7651a6 (patch) | |
tree | 051fa37a2fe30052254bf79a60bd870667fcc0b7 /src/lib/middleware.ts | |
parent | afd9fc4477fff90e5db917f350d99c3d01fba2bd (diff) | |
parent | 1275280a9e3a31f6080079d564a8fb9e1847db8b (diff) |
Merge pull request #135 from lowercasename/rk/public-events
Events and groups optionally visible on front page
Diffstat (limited to 'src/lib/middleware.ts')
-rw-r--r-- | src/lib/middleware.ts | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/src/lib/middleware.ts b/src/lib/middleware.ts index 0594e90..5073137 100644 --- a/src/lib/middleware.ts +++ b/src/lib/middleware.ts @@ -1,14 +1,14 @@ -import { Request, Response } from "express"; +import { NextFunction, Request, Response } from "express"; import MagicLink from "../models/MagicLink.js"; -import getConfig from "../lib/config.js"; - -const config = getConfig(); +import getConfig, { GathioConfig } from "../lib/config.js"; +import { deepMerge } from "../util/object.js"; export const checkMagicLink = async ( req: Request, res: Response, - next: any, + next: NextFunction, ) => { + const config = getConfig(); if (!config.general.creator_email_addresses?.length) { // No creator email addresses are configured, so skip the magic link check return next(); @@ -49,3 +49,22 @@ export const checkMagicLink = async ( } next(); }; + +// Route-specific middleware which injects the config into the request object +// It can also be used to modify the config based on the request, which +// we use for Cypress testing. +export const getConfigMiddleware = ( + req: Request, + res: Response, + next: NextFunction, +) => { + const config = getConfig(); + if (process.env.CYPRESS === "true" && req.cookies?.cypressConfigOverride) { + console.log("Overriding config with Cypress config"); + const override = JSON.parse(req.cookies.cypressConfigOverride); + res.locals.config = deepMerge<GathioConfig>(config, override); + return next(); + } + res.locals.config = config; + return next(); +}; |