'use strict' const nodemailer = require('nodemailer') const SMTP_HOST = process.env.SMTP_HOST || '' const SMTP_PORT = parseInt(process.env.SMTP_PORT || '587', 10) const SMTP_USER = process.env.SMTP_USER || '' const SMTP_PASS = process.env.SMTP_PASS || '' const APP_URL = (process.env.APP_URL || 'http://localhost:3001').replace(/\/$/, '') const FROM_ADDR = SMTP_USER ? `"MorseQuest" <${SMTP_USER}>` : '"MorseQuest" ' let transport = null if (SMTP_HOST && SMTP_USER && SMTP_PASS) { transport = nodemailer.createTransport({ host: SMTP_HOST, port: SMTP_PORT, secure: SMTP_PORT === 465, auth: { user: SMTP_USER, pass: SMTP_PASS }, }) console.log(`[mailer] SMTP configured: ${SMTP_HOST}:${SMTP_PORT}`) } else { console.warn('[mailer] SMTP not configured — magic links will be logged only') } async function send(to, subject, html) { if (!transport) { console.log(`[mailer] Would send to ${to}: ${subject}`) return } await transport.sendMail({ from: FROM_ADDR, to, subject, html }) } async function sendMagicLink(email, token) { const link = `${APP_URL}/api/auth/verify?token=${token}` const subject = 'Your MorseQuest Login Link' const html = `

⚡ MorseQuest Login

Click the button below to log in to MorseQuest. This link is valid for 24 hours and can only be used once.

Begin Your Quest ›

If you didn't request this link, you can ignore this email safely.

` await send(email, subject, html) } module.exports = { sendMagicLink }