Something went wrong. Try again.
A free, open-source, self-hosted place to write a novel.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156import { createTransport, type Transporter } from 'nodemailer';import { MAIL_FROM, SMTP_HOST, SMTP_PASSWORD, SMTP_PORT, SMTP_SECURE, SMTP_USER} from '$app/env/private';import { declaredAddress } from './trusted-origin.ts';
/** * Mail is an option: with no mailer, a reset link goes to the log and the admin * sets a password from the accounts screen. `getMailer` returns null and every * caller has a path for that. */export type MailerConfig = { host: string; port: number; secure: boolean; user?: string; password?: string; from: string;};
export type MailMessage = { to: string; subject: string; text: string;};
export type Mailer = { send: (message: MailMessage) => Promise<void>;};
/** A relay that hangs must not hang the form. Nodemailer takes milliseconds. */const TIMEOUTS = { connectionTimeout: 10_000, greetingTimeout: 10_000, socketTimeout: 20_000};
/** The transport options, built in one place so a test can read them. */export function transportOptions(config: MailerConfig) { return { host: config.host, port: config.port, secure: config.secure, ...TIMEOUTS, ...(config.user && config.password ? { auth: { user: config.user, pass: config.password } } : {}) };}
/** * `transport` is a parameter so a test can hand in a stub and read the message, * with no network. */export function createMailer( config: MailerConfig, transport: Transporter = createTransport(transportOptions(config))): Mailer { return { send: async (message) => { await transport.sendMail({ from: config.from, ...message }); } };}
/** Read once: an environment variable cannot change while the process runs. */let cached: Mailer | null | undefined;
export function getMailer(): Mailer | null { if (cached !== undefined) return cached;
const config = mailerConfigFromEnvironment();
if (!config) { cached = null; return cached; }
cached = createMailer(config);
return cached;}
/** Null unless both a server and a sender are set: half a mailer sends nothing. */export function mailerConfigFromEnvironment(): MailerConfig | null { if (!SMTP_HOST) return null;
if (!MAIL_FROM) { console.warn( '[fablewright] SMTP_HOST is set but MAIL_FROM is not, so no mail will be sent. Password reset links go to this log.' );
return null; }
return { host: SMTP_HOST, port: SMTP_PORT, secure: SMTP_SECURE, user: SMTP_USER, password: SMTP_PASSWORD, from: MAIL_FROM };}
/** Whether a reset link can arrive by mail. The admin page reports this. */export function mailerConfigured(): boolean { return getMailer() !== null;}
/** Both a mailer and a declared address: either one missing sends the link to the log. */export function resetMailReady(): boolean { return mailerConfigured() && declaredAddress() !== undefined;}
/* * better-auth serves the callback under its base path, `/api/auth`, which * `hooks.server.ts` routes to `auth.handler`. A link naming another path 404s. */const RESET_CALLBACK = '/api/auth/reset-password';const RESET_PAGE = '/reset-password';
/** * The declared address goes in front of the link; without one it is a path, good * in the log and unusable in mail. * * Built here, not from better-auth's `url`: with no base URL that URL comes from * the request, and whoever asks for a reset chooses the `Host` header. */export function resetLink(token: string, origin: string | undefined): string { const callback = origin ? `${origin}${RESET_PAGE}` : RESET_PAGE; const path = `${RESET_CALLBACK}/${encodeURIComponent(token)}?callbackURL=${encodeURIComponent(callback)}`;
return origin ? `${origin}${path}` : path;}
/** The product's own words, so they live here, not in the auth configuration. */export function resetMessage(url: string): Omit<MailMessage, 'to'> { return { subject: 'Reset your Fablewright password', text: [ 'Someone asked to reset the password for this account on Fablewright.', '', 'Open this link to choose a new password:', url, '', 'The link works once and expires after an hour. If you did not ask for it, ignore this message: your password stays as it is.' ].join('\n') };}