printhive/lib/mailer.js
dlawler489 5af88eb5ce
All checks were successful
build-and-push / docker (push) Successful in 29s
Add email/password auth + SMTP password resets
- users + password_resets tables; bcrypt hashing; JWT session in an
  httpOnly cookie (auth_secret auto-generated in settings)
- /api gated behind auth (except /api/config and /api/auth/*)
- First-run creates the account (no open registration after that);
  login, logout, forgot/reset-password flows
- SMTP settings (host/port/secure/user/pass/from) in Settings menu;
  nodemailer sends reset links
- Auth screen (sign in / create account / forgot / reset) gates the SPA;
  Sign out button; COOKIE_SECURE=true in compose for HTTPS

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-22 22:33:11 +10:00

30 lines
1.2 KiB
JavaScript

const nodemailer = require('nodemailer');
// Build a transport from the resolved SMTP settings, or null if not configured.
function buildTransport(smtp) {
if (!smtp.host) return null;
const port = Number(smtp.port) || 587;
return nodemailer.createTransport({
host: smtp.host,
port,
secure: smtp.secure === true || smtp.secure === 'true' || port === 465,
auth: smtp.user ? { user: smtp.user, pass: smtp.pass } : undefined,
});
}
// smtp: { host, port, secure, user, pass, from }
async function sendPasswordReset(smtp, to, link) {
const transport = buildTransport(smtp);
if (!transport) throw new Error('SMTP is not configured.');
await transport.sendMail({
from: smtp.from || smtp.user,
to,
subject: 'Printhive password reset',
text: `Reset your Printhive password using this link (valid for 1 hour):\n\n${link}\n\nIf you didn't request this, you can ignore this email.`,
html: `<p>Reset your Printhive password using this link (valid for 1 hour):</p>
<p><a href="${link}">${link}</a></p>
<p style="color:#888">If you didn't request this, you can ignore this email.</p>`,
});
}
module.exports = { buildTransport, sendPasswordReset };