MIF_E31221222/supabase/functions/send-email/index.ts

63 lines
1.6 KiB
TypeScript

import { Webhook } from "https://esm.sh/standardwebhooks@1.0.0";
import { Resend } from "npm:resend";
const resend = new Resend("re_WtXdegYe_Ey3yiShKfZZtjCyY1agkEaSi");
const hookSecret =
"jeroAB/CXdS721OiHV0Ac0yRcxO7eNihgjblH62xMhLBNc6OwK3DQnkbrHjTlSw5anml2onNTolG3SzZ" as string;
Deno.serve(async (req) => {
if (req.method !== "POST") {
return new Response("not allowed", { status: 400 });
}
const payload = await req.text();
const headers = Object.fromEntries(req.headers);
const wh = new Webhook(hookSecret);
try {
const { user, email_data } = wh.verify(payload, headers) as {
user: {
email: string;
};
email_data: {
token: string;
token_hash: string;
redirect_to: string;
email_action_type: string;
site_url: string;
token_new: string;
token_hash_new: string;
};
};
const { error } = await resend.emails.send({
from: "welcome <onboarding@example.com>",
to: [user.email],
subject: "Welcome to my site!",
text: `Confirm you signup with this code: ${email_data.token}`,
});
if (error) {
throw error;
}
} catch (error) {
return new Response(
JSON.stringify({
error: {
http_code: error.code,
message: error.message,
},
}),
{
status: 401,
headers: { "Content-Type": "application/json" },
}
);
}
const responseHeaders = new Headers();
responseHeaders.set("Content-Type", "application/json");
return new Response(JSON.stringify({}), {
status: 200,
headers: responseHeaders,
});
});