diff options
author | Raphael Kabo <raphaelkabo@hey.com> | 2024-02-25 17:56:25 +0000 |
---|---|---|
committer | Raphael Kabo <raphaelkabo@hey.com> | 2024-02-25 17:56:25 +0000 |
commit | cd0f291eb1a608589fcc2c1875fa7099ed8e2c51 (patch) | |
tree | 05b1d8b1d63baed174883cc96807051e530969a2 /src/lib/middleware.ts | |
parent | b17238eb2840553c69fc2dae168be557afbcee9c (diff) |
feat: optionally restrict event creation to specific emails
Diffstat (limited to 'src/lib/middleware.ts')
-rw-r--r-- | src/lib/middleware.ts | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/lib/middleware.ts b/src/lib/middleware.ts new file mode 100644 index 0000000..0594e90 --- /dev/null +++ b/src/lib/middleware.ts @@ -0,0 +1,51 @@ +import { Request, Response } from "express"; +import MagicLink from "../models/MagicLink.js"; +import getConfig from "../lib/config.js"; + +const config = getConfig(); + +export const checkMagicLink = async ( + req: Request, + res: Response, + next: any, +) => { + if (!config.general.creator_email_addresses?.length) { + // No creator email addresses are configured, so skip the magic link check + return next(); + } + if (!req.body.magicLinkToken) { + return res.status(400).json({ + errors: [ + { + message: "No magic link token was provided.", + }, + ], + }); + } + if (!req.body.creatorEmail) { + return res.status(400).json({ + errors: [ + { + message: "No creator email was provided.", + }, + ], + }); + } + const magicLink = await MagicLink.findOne({ + token: req.body.magicLinkToken, + email: req.body.creatorEmail, + expiryTime: { $gt: new Date() }, + permittedActions: "createEvent", + }); + if (!magicLink || magicLink.email !== req.body.creatorEmail) { + return res.status(400).json({ + errors: [ + { + message: + "Magic link is invalid or has expired. Get a new one <a href='/new'>here</a>.", + }, + ], + }); + } + next(); +}; |