This repository has been archived on 2026-09-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
confessions-of-grace/app/api/subscribe/route.ts
T
2026-02-20 09:12:47 -06:00

71 lines
1.8 KiB
TypeScript

import { createClient } from "@/utils/supabase/server";
import { NextRequest, NextResponse } from "next/server";
export async function POST(req: NextRequest) {
const supabase = await createClient();
const { email } = await req.json();
if (!email) {
return NextResponse.json(
{ message: "Email is required." },
{ status: 400 }
);
}
const emailRegex = /\S+@\S+\.\S+/;
if (!emailRegex.test(email)) {
return NextResponse.json(
{ message: "Invalid email address." },
{ status: 400 }
);
}
try {
const { data: existingEmails, error: selectError } = await supabase
.from("subscriptions")
.select("email")
.eq("email", email);
if (selectError) {
console.error("Error checking existing email:", selectError);
return NextResponse.json(
{ message: "An unexpected error occurred while checking email." },
{ status: 500 }
);
}
if (existingEmails && existingEmails.length > 0) {
return NextResponse.json(
{ message: "Email is already subscribed." },
{ status: 400 }
);
}
const { error: insertError } = await supabase
.from("subscriptions")
.insert([{ email }]);
if (insertError) {
console.error("Failed to save subscription:", insertError);
return NextResponse.json(
{
message:
"An unexpected error occurred while saving subscription.",
},
{ status: 500 }
);
}
return NextResponse.json(
{ message: "Successfully subscribed!" },
{ status: 200 }
);
} catch (error) {
console.error("An unexpected server error occurred:", error);
return NextResponse.json(
{ message: "An unexpected error occurred." },
{ status: 500 }
);
}
}