Refactor: Implement trek-captcha generation

This commit is contained in:
2026-03-14 00:29:47 -07:00
parent 7a4b7464df
commit a57099b60a

View File

@@ -1,26 +1,20 @@
import svgCaptcha from 'svg-captcha'; import captcha from 'trek-captcha';
import { NextResponse } from 'next/server'; import { NextResponse } from 'next/server';
import crypto from 'crypto'; import crypto from 'crypto';
// Prevent Next.js from evaluating this route at build time
export const dynamic = 'force-dynamic'; export const dynamic = 'force-dynamic';
export async function GET() { export async function GET() {
const captcha = svgCaptcha.create({ // Generate a 4-character alphanumeric captcha
size: 5, const { token, buffer } = await captcha({ size: 4, style: -1 });
ignoreChars: '0o1i',
noise: 2,
color: true,
background: '#f3f4f6'
});
const text = captcha.text.toLowerCase(); const text = token.toLowerCase();
const secret = process.env.CAPTCHA_SECRET || 'dev_secret_meow'; const secret = process.env.CAPTCHA_SECRET || 'dev_secret_meow';
const hash = crypto.createHmac('sha256', secret).update(text).digest('hex'); const hash = crypto.createHmac('sha256', secret).update(text).digest('hex');
const response = new NextResponse(captcha.data, { const response = new NextResponse(buffer, {
headers: { headers: {
'Content-Type': 'image/svg+xml', 'Content-Type': 'image/gif',
'Cache-Control': 'no-store, max-age=0' 'Cache-Control': 'no-store, max-age=0'
} }
}); });