| // Transactional email via Resend (resend.com). | |
| import { Resend } from "resend"; | |
| import { magicCodeHtml } from "./email-templates"; | |
| const FROM_DEFAULT = "noreply@proteinea.com"; | |
| function getResend(): Resend { | |
| const key = process.env.RESEND_API_KEY; | |
| if (!key) { | |
| throw new Error( | |
| "RESEND_API_KEY is not set. Cannot send emails in production.", | |
| ); | |
| } | |
| return new Resend(key); | |
| } | |
| /** | |
| * Send a magic-link verification code to the given email address. | |
| * Throws on any send failure — caller should catch and return a 500. | |
| */ | |
| export async function sendMagicCode( | |
| email: string, | |
| code: string, | |
| ): Promise<void> { | |
| const resend = getResend(); | |
| const from = process.env.RESEND_FROM ?? FROM_DEFAULT; | |
| const { error } = await resend.emails.send({ | |
| from, | |
| to: email, | |
| subject: "Your Antibody Studio verification code", | |
| html: magicCodeHtml(code), | |
| }); | |
| if (error) { | |
| throw new Error(`Resend API error: ${error.message}`); | |
| } | |
| } | |