import { magicLinkEmail, resetPasswordEmail, verifyEmailEmail } from '../emails/templates'; import { isEmailConfigured, sendEmail } from '../lib/email'; import { env } from '../lib/env'; import { logger } from '../lib/logger'; /** * Auth emails (verification, password reset, magic link) through the app's * SMTP mailer. * * Without SMTP the auth instance does not require email verification, and * these log a warning instead of sending, so password sign-up and sign-in * still work in a fresh project. Magic link is the exception: the link IS * the sign-in, so without SMTP it is switched off (`magicLinkAvailable`). * In development the warning includes the link, since nothing else can * deliver it; elsewhere it never does (the link is a one-time credential). * * A send failure is logged, never thrown: the endpoints answer the same * whether or not an account exists, and an SMTP error must not change that. */ /** * Whether sign-in links can be offered: MAGIC_LINK_ENABLED and an SMTP * server to deliver them. The public settings (routes/v1/auth.ts) show the * option only then, and the send endpoint refuses otherwise * (auth/index.ts), so nobody waits for an email that never comes. */ export function magicLinkAvailable(): boolean { return env.MAGIC_LINK_ENABLED && isEmailConfigured(); } type AuthEmailKind = 'verification' | 'password reset' | 'magic link'; const TEMPLATES: Record { subject: string; html: string }> = { verification: verifyEmailEmail, 'password reset': resetPasswordEmail, 'magic link': magicLinkEmail, }; export async function sendAuthEmail(kind: AuthEmailKind, to: string, url: string): Promise { if (!isEmailConfigured()) { logger.warn( env.NODE_ENV === 'development' ? { kind, to, url } : { kind, to }, `SMTP is not configured, so the ${kind} email was not sent. Set SMTP_* (vibecarbon configure) to deliver auth emails.` ); return; } const { subject, html } = TEMPLATES[kind]({ url }); try { await sendEmail({ to, subject, html }); } catch { // sendEmail already logged the SMTP error. logger.error({ kind, to }, `The ${kind} email could not be sent`); } }