forked from codechefPesuecc/CodeChef-PESUECC-Chapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
49 lines (43 loc) · 1.49 KB
/
Copy pathroute.ts
File metadata and controls
49 lines (43 loc) · 1.49 KB
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
import { NextResponse } from "next/server";
import { getCurrentUser } from "@/server/auth/session";
import { verifyOtp } from "@/server/auth/verification";
import { clientIp, enforceRateLimits } from "@/server/rateLimit";
export const dynamic = "force-dynamic";
export async function POST(req: Request) {
const user = await getCurrentUser();
if (!user) {
return NextResponse.json(
{ ok: false, error: "Log in first.", needsAuth: true },
{ status: 401 },
);
}
if (user.emailVerified) {
return NextResponse.json({ ok: true, alreadyVerified: true });
}
// Route-level throttle on top of the per-OTP 5-attempt cap: blunts brute-force
// guessing (and the DB writes each wrong guess makes), since a resend resets
// the per-code counter.
const limited = await enforceRateLimits([
[`verify:user:${user.id}`, 10, 10 * 60_000],
[`verify:ip:${clientIp(req)}`, 30, 10 * 60_000],
]);
if (limited) return limited;
let body: { code?: string };
try {
body = await req.json();
} catch {
return NextResponse.json({ ok: false, error: "Invalid JSON." }, { status: 400 });
}
const code = String(body.code ?? "").trim();
if (!/^\d{6}$/.test(code)) {
return NextResponse.json(
{ ok: false, error: "Enter the 6-digit code." },
{ status: 400 },
);
}
const result = await verifyOtp(user.id, code);
if (!result.ok) {
return NextResponse.json({ ok: false, error: result.error }, { status: 400 });
}
return NextResponse.json({ ok: true });
}