summaryrefslogtreecommitdiff
path: root/apps/web/src/app/api/waitlist/token
diff options
context:
space:
mode:
authorMaze <167211895+mazeincoding@users.noreply.github.com>2025-07-16 12:00:11 +0200
committerGitHub <noreply@github.com>2025-07-16 12:00:11 +0200
commitccca2e8d0f94847c3877202d2940bcf57c327dbc (patch)
tree4818e76a0838f94537024c0ef41d1b328d969c04 /apps/web/src/app/api/waitlist/token
parent2982293b54129e23d103052bea640ea8a45812e9 (diff)
parent0cb26ef088d7b813bef4fd702ac26b813f1a9db1 (diff)
downloadopencut-ccca2e8d0f94847c3877202d2940bcf57c327dbc.tar.gz
Merge pull request #308 from enkeii64/feat/display-all-contributors
feat: display all contributors
Diffstat (limited to 'apps/web/src/app/api/waitlist/token')
-rw-r--r--apps/web/src/app/api/waitlist/token/route.ts46
1 files changed, 46 insertions, 0 deletions
diff --git a/apps/web/src/app/api/waitlist/token/route.ts b/apps/web/src/app/api/waitlist/token/route.ts
new file mode 100644
index 00000000..4c7695fa
--- /dev/null
+++ b/apps/web/src/app/api/waitlist/token/route.ts
@@ -0,0 +1,46 @@
+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 });
+}