2024-08-26 13:11:47 +02:00
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
import clientPromise from "@/lib/db";
|
2024-09-08 05:44:44 +02:00
|
|
|
import { Collection } from "mongodb";
|
|
|
|
import dns from "dns/promises";
|
|
|
|
import { isEmail } from "validator";
|
2024-08-24 06:28:25 +02:00
|
|
|
|
2024-08-26 13:11:47 +02:00
|
|
|
export async function POST(req: NextRequest) {
|
2024-09-08 05:44:44 +02:00
|
|
|
const generateUnsubscribeID = () => {
|
|
|
|
const chars =
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
|
|
let result = "";
|
|
|
|
for (let i = 0; i < 32; i++) {
|
|
|
|
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
const generateUniqueUnsubscribeID = async (collection: Collection) => {
|
2024-09-08 14:57:02 +02:00
|
|
|
const id: string = generateUnsubscribeID();
|
2024-09-08 05:44:44 +02:00
|
|
|
const result = await collection
|
|
|
|
.find({
|
|
|
|
unsubscribeId: id
|
|
|
|
})
|
|
|
|
.toArray();
|
|
|
|
if (result.length > 0) {
|
2024-09-08 14:57:02 +02:00
|
|
|
const newId: string = await generateUniqueUnsubscribeID(collection);
|
|
|
|
return newId;
|
2024-09-08 05:44:44 +02:00
|
|
|
}
|
|
|
|
return id;
|
|
|
|
};
|
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
try {
|
|
|
|
const { email, captchaToken } = await req.json();
|
|
|
|
|
|
|
|
if (!email || !captchaToken) {
|
|
|
|
return NextResponse.json(
|
|
|
|
{ message: "Email and captcha token are required." },
|
|
|
|
{ status: 400 }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify hCaptcha token
|
|
|
|
const hcaptchaResponse = await fetch(
|
|
|
|
`https://api.hcaptcha.com/siteverify`,
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
|
|
},
|
|
|
|
body: `secret=${process.env.HCAPTCHA_SECRET}&response=${captchaToken}`
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const hcaptchaData = await hcaptchaResponse.json();
|
|
|
|
|
|
|
|
if (!hcaptchaData.success) {
|
|
|
|
return NextResponse.json(
|
|
|
|
{ message: "Captcha verification failed." },
|
|
|
|
{ status: 400 }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-09-08 05:44:44 +02:00
|
|
|
// Check email address
|
|
|
|
if (!isEmail(email)) {
|
|
|
|
return NextResponse.json(
|
|
|
|
{ message: "Invalid email address" },
|
|
|
|
{ status: 400 }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check email host
|
|
|
|
const emailDomainMatch = email.match(/@([^@]+)/);
|
|
|
|
const emailDomain = emailDomainMatch ? emailDomainMatch[1] : "";
|
|
|
|
let isEmailHostValid = false;
|
|
|
|
try {
|
|
|
|
const mxRecords = await dns.resolveMx(emailDomain);
|
2024-09-17 07:12:23 +02:00
|
|
|
if (mxRecords.length > 0) {
|
|
|
|
for (let i = 0; i < mxRecords.length; i++) {
|
|
|
|
try {
|
|
|
|
const aRecords = await dns.resolve4(mxRecords[i].exchange);
|
|
|
|
if (aRecords.length > 0) {
|
|
|
|
isEmailHostValid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (err) {}
|
|
|
|
try {
|
|
|
|
const aaaaRecords = await dns.resolve6(mxRecords[i].exchange);
|
|
|
|
if (aaaaRecords.length > 0) {
|
|
|
|
isEmailHostValid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (err) {}
|
|
|
|
}
|
|
|
|
}
|
2024-09-08 05:44:44 +02:00
|
|
|
} catch (err) {}
|
|
|
|
if (!isEmailHostValid) {
|
|
|
|
return NextResponse.json(
|
|
|
|
{ message: "Email domain is misconfigured" },
|
|
|
|
{ status: 400 }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
const client = await clientPromise;
|
2024-09-08 05:44:44 +02:00
|
|
|
const db = client.db(process.env.MONGODB_DB);
|
2024-09-07 09:12:48 +02:00
|
|
|
const collection = db.collection("subscribers");
|
|
|
|
|
|
|
|
// checking if email alr exists
|
|
|
|
const existingSubscriber = await collection.findOne({ email });
|
|
|
|
if (existingSubscriber) {
|
|
|
|
return NextResponse.json(
|
|
|
|
{ message: "This email is already subscribed." },
|
|
|
|
{ status: 409 }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// saves the email in the db
|
2024-09-08 05:44:44 +02:00
|
|
|
await collection.insertOne({
|
|
|
|
email,
|
|
|
|
subscribedAt: new Date(),
|
|
|
|
unsubscribeId: await generateUniqueUnsubscribeID(collection)
|
|
|
|
});
|
2024-09-07 09:12:48 +02:00
|
|
|
|
|
|
|
return NextResponse.json(
|
|
|
|
{ message: "Successfully subscribed!" },
|
|
|
|
{ status: 201 }
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error subscribing:", error);
|
|
|
|
return NextResponse.json(
|
|
|
|
{ message: "Internal Server Error" },
|
|
|
|
{ status: 500 }
|
|
|
|
);
|
|
|
|
}
|
2024-08-24 06:28:25 +02:00
|
|
|
}
|