mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 11:51:14 -05:00
Refactor: Continue implementation plan
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import { serve } from "https://deno.land/std@0.190.0/http/server.ts";
|
||||
import { startRequest, endRequest } from "../_shared/logger.ts";
|
||||
|
||||
const corsHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
||||
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type, x-request-id',
|
||||
};
|
||||
|
||||
// Comprehensive list of disposable email domains
|
||||
@@ -74,18 +75,27 @@ const handler = async (req: Request): Promise<Response> => {
|
||||
return new Response(null, { headers: corsHeaders });
|
||||
}
|
||||
|
||||
const tracking = startRequest('validate-email');
|
||||
|
||||
try {
|
||||
const { email }: ValidateEmailRequest = await req.json();
|
||||
|
||||
if (!email || typeof email !== 'string') {
|
||||
endRequest(tracking, 400, 'Email address is required');
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
valid: false,
|
||||
reason: 'Email address is required'
|
||||
reason: 'Email address is required',
|
||||
requestId: tracking.requestId
|
||||
} as ValidationResult),
|
||||
{
|
||||
status: 400,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
|
||||
headers: {
|
||||
...corsHeaders,
|
||||
'Content-Type': 'application/json',
|
||||
'X-Request-ID': tracking.requestId
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -93,14 +103,21 @@ const handler = async (req: Request): Promise<Response> => {
|
||||
// Basic email format validation
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
if (!emailRegex.test(email)) {
|
||||
endRequest(tracking, 400, 'Invalid email format');
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
valid: false,
|
||||
reason: 'Invalid email format'
|
||||
reason: 'Invalid email format',
|
||||
requestId: tracking.requestId
|
||||
} as ValidationResult),
|
||||
{
|
||||
status: 400,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
|
||||
headers: {
|
||||
...corsHeaders,
|
||||
'Content-Type': 'application/json',
|
||||
'X-Request-ID': tracking.requestId
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -111,6 +128,9 @@ const handler = async (req: Request): Promise<Response> => {
|
||||
// Check if domain is disposable
|
||||
if (DISPOSABLE_DOMAINS.has(domain)) {
|
||||
console.log(`Blocked disposable email domain: ${domain}`);
|
||||
|
||||
endRequest(tracking, 400, 'Disposable email domain blocked');
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
valid: false,
|
||||
@@ -119,37 +139,58 @@ const handler = async (req: Request): Promise<Response> => {
|
||||
'Use a personal email (Gmail, Outlook, Yahoo, etc.)',
|
||||
'Use your work or school email address',
|
||||
'Use an email from your own domain'
|
||||
]
|
||||
],
|
||||
requestId: tracking.requestId
|
||||
} as ValidationResult),
|
||||
{
|
||||
status: 400,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
|
||||
headers: {
|
||||
...corsHeaders,
|
||||
'Content-Type': 'application/json',
|
||||
'X-Request-ID': tracking.requestId
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Email is valid
|
||||
console.log(`Email validated successfully: ${email}`);
|
||||
|
||||
endRequest(tracking, 200);
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
valid: true
|
||||
valid: true,
|
||||
requestId: tracking.requestId
|
||||
} as ValidationResult),
|
||||
{
|
||||
status: 200,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
|
||||
headers: {
|
||||
...corsHeaders,
|
||||
'Content-Type': 'application/json',
|
||||
'X-Request-ID': tracking.requestId
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
} catch (error: any) {
|
||||
console.error('Error in validate-email function:', error);
|
||||
|
||||
endRequest(tracking, 500, error.message);
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
valid: false,
|
||||
reason: 'Internal server error during email validation'
|
||||
reason: 'Internal server error during email validation',
|
||||
requestId: tracking.requestId
|
||||
} as ValidationResult),
|
||||
{
|
||||
status: 500,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
|
||||
headers: {
|
||||
...corsHeaders,
|
||||
'Content-Type': 'application/json',
|
||||
'X-Request-ID': tracking.requestId
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user