summaryrefslogtreecommitdiff
path: root/apps/web/src/app/api/waitlist/token/route.ts
blob: fc2ff32cb2ee3cfc16962c3e2902613d0eaa952f (plain) (blame)
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
52
53
54
55
56
57
58
59
60
61
import { NextRequest, NextResponse } from "next/server";
import { cookies } from "next/headers";
import crypto from "crypto";
import { env } from "@/env";

const CSRF_TOKEN_NAME = "waitlist-csrf";
const TOKEN_EXPIRY = 60 * 60 * 1000;
const allowedHosts =
  env.NODE_ENV === "development"
    ? ["localhost:3000", "127.0.0.1:3000"]
    : ["opencut.app", "www.opencut.app"];

export async function GET(request: NextRequest) {
  const referer = request.headers.get("referer");
  const host = request.headers.get("host");

  if (referer) {
    const refererUrl = new URL(referer);

    if (
      !allowedHosts.some(
        (allowed) =>
          refererUrl.host === allowed || refererUrl.host.endsWith(allowed)
      )
    ) {
      return NextResponse.json({ error: "Forbidden" }, { status: 403 });
    }
  } else if (host) {
    if (
      !allowedHosts.some(
        (allowed) => host === allowed || host.endsWith(allowed)
      )
    ) {
      return NextResponse.json({ error: "Forbidden" }, { status: 403 });
    }
  } else {
    return NextResponse.json({ error: "Forbidden" }, { status: 403 });
  }

  if (!env.BETTER_AUTH_SECRET) {
    throw new Error("BETTER_AUTH_SECRET must be configured");
  }

  const token = crypto.randomBytes(32).toString("hex");
  const timestamp = Date.now();
  const signature = crypto
    .createHmac("sha256", env.BETTER_AUTH_SECRET)
    .update(`${token}:${timestamp}`)
    .digest("hex");

  const cookieStore = await cookies();
  cookieStore.set(CSRF_TOKEN_NAME, `${token}:${timestamp}:${signature}`, {
    httpOnly: true,
    secure: env.NODE_ENV === "production",
    sameSite: "strict",
    maxAge: TOKEN_EXPIRY / 1000,
    path: "/",
  });

  return NextResponse.json({ token });
}