summaryrefslogtreecommitdiff
path: root/src/lib/middleware.ts
blob: 0594e9079490984febc188c3fd9bf943b7a5dd65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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();
};