// Vercel Serverless Function: Double Opt-In Email Verification // Engineered for Aira Group of Technology (airaaigroup.in) import crypto from "crypto"; import { getWelcomeEmailHtml } from "./_emailTemplates.js"; import { sendEmail } from "./_mailer.js"; const SECRET = process.env.VERIFY_SECRET || "aira-sovereign-secret-token-key-2026"; const HOSTINGER_TOKEN = process.env.HOSTINGER_API_TOKEN || "QVeJgNyXciHezaw5fsjAJeTiEXJfBfd4FqwUifna8cfab21c"; const PROFILE_UUID = "623e0772-031e-44fc-8a18-d225af88bad6"; export function generateVerifyToken(email) { return crypto.createHmac("sha256", SECRET).update(email.trim().toLowerCase()).digest("hex"); } export default async function handler(req, res) { const { email, token, name } = req.query || {}; if (!email || !token) { return res.status(400).send(renderStatusHtml(false, "Invalid Verification Link", "The confirmation link is missing required parameters. Please subscribe again from our portal.")); } const cleanEmail = email.trim().toLowerCase(); const displayName = name ? decodeURIComponent(name).trim() : ""; const expectedToken = generateVerifyToken(cleanEmail); if (token !== expectedToken) { return res.status(403).send(renderStatusHtml(false, "Verification Link Invalid", "This confirmation link is invalid or has expired. Please subscribe again from our portal.")); } // 1. Sync confirmed subscriber with Hostinger Reach try { const reachRes = await fetch(`https://developers.hostinger.com/api/reach/v1/profiles/${PROFILE_UUID}/contacts`, { method: "POST", headers: { "Authorization": `Bearer ${HOSTINGER_TOKEN}`, "Content-Type": "application/json", "User-Agent": "AiraGroup-Portal/1.0" }, body: JSON.stringify({ email: cleanEmail, name: displayName || null, subscription_status: "subscribed" }) }); console.log("[Hostinger Reach Confirmed Sync]:", reachRes.status); } catch (err) { console.error("[Hostinger Reach Sync Error]:", err.message); } // 2. Dispatch Welcome Email try { const welcomeHtml = getWelcomeEmailHtml(displayName); await sendEmail({ to: cleanEmail, toName: displayName, subject: "Welcome to Aira Group — Sovereign Engineering. Zero Dependency", html: welcomeHtml }); } catch (err) { console.error("[Welcome Email Dispatch Error]:", err.message); } // 3. Render Clean Corporate Success Page return res.status(200).send(renderStatusHtml( true, "Subscription Confirmed! 🎉", `Thank you for confirming your email (${escapeHtml(cleanEmail)}). You are now an active member of Aira Group Sovereign Engineering Dispatches. Your official welcome dispatch has been sent to your inbox.` )); } function renderStatusHtml(isSuccess, title, message) { const accentColor = isSuccess ? "#10b981" : "#ef4444"; const icon = isSuccess ? "✓" : "!"; return ` ${escapeHtml(title)} — Aira Group of Technology
${icon}

${escapeHtml(title)}

${message}

← Return to Aira Group Homepage
`; } function escapeHtml(str) { return String(str) .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); }