feat(newsletter): implement sovereign double opt-in confirmation and welcome workflow
This commit is contained in:
1 parent
080faa5ffc
commit
9792a403cb
5 files changed
+566
-24
No files matched your search
+41
-24
@@ -1,8 +1,15 @@
|
||||
// Vercel Serverless Function: Automated Hostinger Reach Subscription
|
||||
// Vercel Serverless Function: Automated Double Opt-In Subscription
|
||||
// Engineered for Aira Group of Technology (airaaigroup.in)
|
||||
|
||||
import { generateVerifyToken } from "./verify.js";
|
||||
import { getConfirmationEmailHtml } from "./_emailTemplates.js";
|
||||
import { sendEmail } from "./_mailer.js";
|
||||
|
||||
const HOSTINGER_TOKEN = process.env.HOSTINGER_API_TOKEN || "QVeJgNyXciHezaw5fsjAJeTiEXJfBfd4FqwUifna8cfab21c";
|
||||
const PROFILE_UUID = "623e0772-031e-44fc-8a18-d225af88bad6";
|
||||
|
||||
export default async function handler(req, res) {
|
||||
// Set CORS headers
|
||||
// CORS headers
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
res.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
|
||||
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
||||
@@ -22,38 +29,48 @@ export default async function handler(req, res) {
|
||||
return res.status(400).json({ error: "Valid email address is required." });
|
||||
}
|
||||
|
||||
const HOSTINGER_TOKEN = process.env.HOSTINGER_API_TOKEN || "QVeJgNyXciHezaw5fsjAJeTiEXJfBfd4FqwUifna8cfab21c";
|
||||
const PROFILE_UUID = "623e0772-031e-44fc-8a18-d225af88bad6";
|
||||
const REACH_URL = `https://developers.hostinger.com/api/reach/v1/profiles/${PROFILE_UUID}/contacts`;
|
||||
const cleanEmail = email.trim().toLowerCase();
|
||||
const cleanName = name ? name.trim() : "";
|
||||
|
||||
const payload = {
|
||||
email: email.trim().toLowerCase(),
|
||||
name: name ? name.trim() : null
|
||||
};
|
||||
// 1. Generate secure cryptographic verification link
|
||||
const token = generateVerifyToken(cleanEmail);
|
||||
const host = req.headers["x-forwarded-host"] || req.headers.host || "www.airaaigroup.in";
|
||||
const proto = req.headers["x-forwarded-proto"] || "https";
|
||||
const verifyUrl = `${proto}://${host}/api/verify?email=${encodeURIComponent(cleanEmail)}&token=${token}${cleanName ? `&name=${encodeURIComponent(cleanName)}` : ""}`;
|
||||
|
||||
const hostingerRes = await fetch(REACH_URL, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Authorization": `Bearer ${HOSTINGER_TOKEN}`,
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "AiraGroup-Portal/1.0"
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
// 2. Dispatch Confirmation Email
|
||||
const confirmHtml = getConfirmationEmailHtml(cleanName, verifyUrl);
|
||||
const mailResult = await sendEmail({
|
||||
to: cleanEmail,
|
||||
toName: cleanName,
|
||||
subject: "Verify Your Subscription — Aira Group of Technology",
|
||||
html: confirmHtml
|
||||
});
|
||||
|
||||
const responseText = await hostingerRes.text();
|
||||
console.log("[Confirmation Mail Dispatch Result]:", mailResult);
|
||||
|
||||
if (!hostingerRes.ok) {
|
||||
console.error("[Hostinger Reach Error]:", hostingerRes.status, responseText);
|
||||
return res.status(hostingerRes.status).json({
|
||||
error: "Failed to sync subscription with Hostinger.",
|
||||
details: responseText
|
||||
// 3. Register contact 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: cleanName || null
|
||||
})
|
||||
});
|
||||
console.log("[Hostinger Reach Contact Sync]:", reachRes.status);
|
||||
} catch (err) {
|
||||
console.warn("[Hostinger Sync Warning]:", err.message);
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: "Subscription confirmed! Please check your email to verify your subscription."
|
||||
message: "Confirmation email sent! Please check your inbox and click the verification button."
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("[Subscribe API Exception]:", err);
|
||||
|
||||
Reference in new issue
Block a user