webhook calls moved server-side

This commit is contained in:
Owen Rees
2023-09-10 20:23:42 +02:00
parent afdb57bd22
commit 4984639f23
3 changed files with 36 additions and 8 deletions
@@ -0,0 +1,34 @@
import { NextRequest, NextResponse } from "next/server";
export async function POST(req: NextRequest) {
const webhookURL = process.env.DISCORD_WEBHOOK_URL;
const { embeds } = await req.json();
if (!webhookURL) {
return NextResponse.json(
{ error: `Discord webhook URL not found` },
{ status: 404 },
);
}
try {
const webhookBody = {
embeds: embeds,
};
const webhookInfo = await fetch(webhookURL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(webhookBody),
});
return NextResponse.json(
{ success: `Message delivered to ${webhookInfo}` },
{ status: 200 },
);
} catch (error) {
return NextResponse.json({ error: `Connection refused` }, { status: 404 });
}
}
+1 -1
View File
@@ -29,8 +29,8 @@ export const handleTournamentSubmit = async (
setIsSending(true);
try {
await tournamentFormToDB(formRefs); // write to DB
await discordWebhook(formRefs); // send Discord notification
await tournamentFormToDB(formRefs); // write to DB
setIsSending(false);
setResponseMessage({
isSuccessful: true,
+1 -7
View File
@@ -10,12 +10,6 @@ const discordWebhook = async ({
yourEmailRef,
messageRef,
}: TournamentFormProps) => {
const webhookURL = process.env.NEXT_PUBLIC_DISCORD_WEBHOOK;
if (!webhookURL) {
throw new Error("Discord webhook URL is not defined");
}
const webhookBody = {
embeds: [
{
@@ -36,7 +30,7 @@ const discordWebhook = async ({
],
};
return await fetch(webhookURL, {
return await fetch("/api/send-discord-notification", {
method: "POST",
headers: {
"Content-Type": "application/json",