fix one-click unsub from the header (#195)

This commit is contained in:
KM Koushik
2025-08-17 09:09:43 +10:00
committed by GitHub
parent bb2a4287cf
commit 43d99bb980
3 changed files with 63 additions and 1 deletions

View File

@@ -24,6 +24,7 @@ export default function SesConfigurations() {
<TableHeader className="">
<TableRow className=" bg-muted/30">
<TableHead className="rounded-tl-xl">Region</TableHead>
<TableHead>Prefix Key</TableHead>
<TableHead>Callback URL</TableHead>
<TableHead>Callback status</TableHead>
<TableHead>Created at</TableHead>
@@ -52,6 +53,7 @@ export default function SesConfigurations() {
sesSettingsQuery.data?.map((sesSetting) => (
<TableRow key={sesSetting.id}>
<TableCell>{sesSetting.region}</TableCell>
<TableCell>{sesSetting.idPrefix}</TableCell>
<TableCell>
<div className="w-[200px] overflow-hidden text-ellipsis">
<TextWithCopyButton

View File

@@ -0,0 +1,49 @@
import { NextRequest, NextResponse } from "next/server";
import { unsubscribeContactFromLink } from "~/server/service/campaign-service";
import { logger } from "~/server/logger/log";
export async function POST(request: NextRequest) {
try {
const url = new URL(request.url);
const id = url.searchParams.get("id");
const hash = url.searchParams.get("hash");
if (!id || !hash) {
logger.warn(
`One-click unsubscribe: Missing id or hash id: ${id} hash: ${hash} url: ${request.url}`
);
return NextResponse.json(
{ error: "Invalid unsubscribe link" },
{ status: 400 }
);
}
// Process the unsubscribe using existing logic
const contact = await unsubscribeContactFromLink(id, hash);
logger.info(
{ contactId: contact.id, campaignId: id.split("-")[1] },
"One-click unsubscribe successful"
);
// Return success response for email clients
return NextResponse.json(
{
success: true,
message: "Successfully unsubscribed",
},
{ status: 200 }
);
} catch (error) {
logger.error(
{ error: error instanceof Error ? error.message : error },
"One-click unsubscribe failed"
);
// Return error response
return NextResponse.json(
{ error: "Failed to process unsubscribe request" },
{ status: 500 }
);
}
}

View File

@@ -99,6 +99,16 @@ export function createUnsubUrl(contactId: string, campaignId: string) {
return `${env.NEXTAUTH_URL}/unsubscribe?id=${unsubId}&hash=${unsubHash}`;
}
export function createOneClickUnsubUrl(contactId: string, campaignId: string) {
const unsubId = `${contactId}-${campaignId}`;
const unsubHash = createHash("sha256")
.update(`${unsubId}-${env.NEXTAUTH_SECRET}`)
.digest("hex");
return `${env.NEXTAUTH_URL}/api/unsubscribe-oneclick?id=${unsubId}&hash=${unsubHash}`;
}
export async function unsubscribeContactFromLink(id: string, hash: string) {
const [contactId, campaignId] = id.split("-");
@@ -253,6 +263,7 @@ async function processContactEmail(jobData: CampaignEmailJob) {
const renderer = new EmailRenderer(jsonContent);
const unsubscribeUrl = createUnsubUrl(contact.id, emailConfig.campaignId);
const oneClickUnsubUrl = createOneClickUnsubUrl(contact.id, emailConfig.campaignId);
// Check for suppressed emails before processing
const toEmails = [contact.email];
@@ -387,7 +398,7 @@ async function processContactEmail(jobData: CampaignEmailJob) {
emailConfig.teamId,
emailConfig.region,
false,
unsubscribeUrl
oneClickUnsubUrl
);
}