From 8fd72e66113f5546af82b5e34ee481513ad44a51 Mon Sep 17 00:00:00 2001 From: multipleof4 Date: Sat, 14 Mar 2026 00:03:27 -0700 Subject: [PATCH] Feat: Create SVG captcha generation route --- app/api/captcha/route.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 app/api/captcha/route.js diff --git a/app/api/captcha/route.js b/app/api/captcha/route.js new file mode 100644 index 0000000..2874d30 --- /dev/null +++ b/app/api/captcha/route.js @@ -0,0 +1,33 @@ +import svgCaptcha from 'svg-captcha'; +import { NextResponse } from 'next/server'; +import crypto from 'crypto'; + +export async function GET() { + const captcha = svgCaptcha.create({ + size: 5, + ignoreChars: '0o1i', + noise: 2, + color: true, + background: '#f3f4f6' + }); + + const text = captcha.text.toLowerCase(); + const secret = process.env.CAPTCHA_SECRET || 'dev_secret_meow'; + const hash = crypto.createHmac('sha256', secret).update(text).digest('hex'); + + const response = new NextResponse(captcha.data, { + headers: { + 'Content-Type': 'image/svg+xml', + 'Cache-Control': 'no-store, max-age=0' + } + }); + + // Store the expected hash in an HttpOnly cookie + response.cookies.set('captcha_hash', hash, { + httpOnly: true, + path: '/', + maxAge: 300 // 5 minutes validity + }); + + return response; +}