website poc
This commit is contained in:
@@ -1,79 +1,107 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { kv } from '@vercel/kv';
|
||||
import { validateEmail, isDisposableEmail } from '@/lib/email-validation';
|
||||
import { checkRateLimit, getClientIp } from '@/lib/rate-limit';
|
||||
|
||||
/**
|
||||
* Email signup storage key
|
||||
*/
|
||||
const SIGNUP_LIST_KEY = 'signups:emails';
|
||||
const isDev = !process.env.KV_REST_API_URL;
|
||||
|
||||
// In-memory fallback for development
|
||||
const devSignups = new Map<string, { email: string; timestamp: string; ip: string }>();
|
||||
|
||||
/**
|
||||
* POST /api/signup
|
||||
* Handle email signup submissions
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
// Parse request body
|
||||
const body = await request.json();
|
||||
const { email } = body;
|
||||
|
||||
if (!email || typeof email !== 'string') {
|
||||
return NextResponse.json(
|
||||
{ error: 'Email is required' },
|
||||
{ error: "That email doesn't look right." },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Validate email format
|
||||
const validation = validateEmail(email);
|
||||
if (!validation.success) {
|
||||
return NextResponse.json(
|
||||
{ error: validation.error },
|
||||
{ error: "That email doesn't look right." },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const sanitizedEmail = validation.email!;
|
||||
|
||||
// Check for disposable email
|
||||
if (isDisposableEmail(sanitizedEmail)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Disposable email addresses are not allowed' },
|
||||
{ error: "That email doesn't look right." },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Rate limiting
|
||||
const clientIp = getClientIp(request);
|
||||
const rateLimitResult = await checkRateLimit(clientIp);
|
||||
|
||||
if (!rateLimitResult.allowed) {
|
||||
const retrySeconds = Math.ceil((rateLimitResult.resetAt - Date.now()) / 1000);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Too many requests. Please try again later.',
|
||||
error: 'Too fast. Try again in a minute.',
|
||||
resetAt: rateLimitResult.resetAt,
|
||||
retryAfter: retrySeconds,
|
||||
},
|
||||
{
|
||||
status: 429,
|
||||
headers: {
|
||||
'Retry-After': Math.ceil((rateLimitResult.resetAt - Date.now()) / 1000).toString(),
|
||||
'Retry-After': retrySeconds.toString(),
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Check if email already exists
|
||||
if (isDev) {
|
||||
console.warn('[DEV MODE] Using in-memory signup storage - data will not persist');
|
||||
|
||||
if (devSignups.has(sanitizedEmail)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Already got you. I'll keep you posted." },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
const signupData = {
|
||||
email: sanitizedEmail,
|
||||
timestamp: new Date().toISOString(),
|
||||
ip: clientIp,
|
||||
};
|
||||
|
||||
devSignups.set(sanitizedEmail, signupData);
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: 'Thanks. That means a lot.',
|
||||
},
|
||||
{
|
||||
status: 200,
|
||||
headers: {
|
||||
'X-RateLimit-Remaining': rateLimitResult.remaining.toString(),
|
||||
'X-RateLimit-Reset': rateLimitResult.resetAt.toString(),
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Production: Use Vercel KV
|
||||
const { kv } = await import('@vercel/kv');
|
||||
|
||||
const existingSignup = await kv.hget(SIGNUP_LIST_KEY, sanitizedEmail);
|
||||
|
||||
if (existingSignup) {
|
||||
return NextResponse.json(
|
||||
{ error: 'This email is already registered' },
|
||||
{ error: "Already got you. I'll keep you posted." },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
// Store email with timestamp
|
||||
const signupData = {
|
||||
email: sanitizedEmail,
|
||||
timestamp: new Date().toISOString(),
|
||||
@@ -84,11 +112,10 @@ export async function POST(request: NextRequest) {
|
||||
[sanitizedEmail]: JSON.stringify(signupData),
|
||||
});
|
||||
|
||||
// Return success response
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: 'Successfully added to waitlist',
|
||||
message: 'Thanks. That means a lot.',
|
||||
},
|
||||
{
|
||||
status: 200,
|
||||
@@ -102,7 +129,7 @@ export async function POST(request: NextRequest) {
|
||||
console.error('Signup error:', error);
|
||||
|
||||
return NextResponse.json(
|
||||
{ error: 'An error occurred. Please try again.' },
|
||||
{ error: 'Something broke. Try again?' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user