SMTP settings on the Settings page, with a test (1.2.0)

SMTP server, port, user, password and from address are now Hub settings in
the database instead of SMTP_* in .env. The password is encrypted with
HUB_ENCRYPTION_KEY and never sent to the browser; a blank field keeps it.
POST /api/admin/settings/smtp-test logs in with the form's values (saved or
not) and sends a test email, returning the server's reason on failure; it is
rate limited and audited. On first start an existing SMTP_* setup is copied
into Settings once. The audit log no longer records the SMTP password.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ls1vMaqhrehXcHJe32jLL9
This commit is contained in:
2026-10-28 01:26:41 +02:00
co-authored by Claude Opus 5.5
parent ad01328ce3
commit 613a4450cb
13 changed files with 298 additions and 35 deletions
+60 -2
View File
@@ -26,6 +26,9 @@ export default function SettingsPage() {
const [audit, setAudit] = useState<AuditRow[]>([]);
const [pw, setPw] = useState({ currentPassword: "", newPassword: "" });
const [msg, setMsg] = useState<Msg>(null);
const [smtpPass, setSmtpPass] = useState({ value: "", clear: false });
const [testTo, setTestTo] = useState("");
const [testing, setTesting] = useState(false);
useEffect(() => {
api<{ settings: Settings; publicKey: string; smtp: boolean }>("/api/admin/settings").then(r => { setSettings(r.settings); setPublicKey(r.publicKey); setSmtp(r.smtp); }).catch(e => setMsg({ type: "err", text: errorText(e) }));
@@ -37,12 +40,39 @@ export default function SettingsPage() {
const save = async (e: React.FormEvent) => {
e.preventDefault();
try {
const r = await api<{ settings: Settings }>("/api/admin/settings", { method: "PUT", body: { settings } });
const r = await api<{ settings: Settings; smtp: boolean }>("/api/admin/settings", { method: "PUT", body: { settings } });
setSettings(r.settings);
setSmtp(r.smtp);
setMsg({ type: "ok", text: "Settings saved." });
} catch (err) { setMsg({ type: "err", text: errorText(err) }); }
};
const smtpValues = () => ({
smtp_host: settings.smtp_host, smtp_port: settings.smtp_port, smtp_user: settings.smtp_user, smtp_from: settings.smtp_from,
smtp_pass: smtpPass.value, smtp_pass_clear: smtpPass.clear,
});
const saveSmtp = async (e: React.FormEvent) => {
e.preventDefault();
try {
const r = await api<{ settings: Settings; smtp: boolean }>("/api/admin/settings", { method: "PUT", body: { settings: smtpValues() } });
setSettings(r.settings);
setSmtp(r.smtp);
setSmtpPass({ value: "", clear: false });
setMsg({ type: "ok", text: "Email settings saved." });
} catch (err) { setMsg({ type: "err", text: errorText(err) }); }
};
const testSmtp = async () => {
setTesting(true);
setMsg(null);
try {
const r = await api<{ to: string }>("/api/admin/settings/smtp-test", { method: "POST", body: { settings: smtpValues(), to: testTo } });
setMsg({ type: "ok", text: `Logged in to the mail server and sent a test email to ${r.to}. Check it arrived (and the spam folder).` });
} catch (err) { setMsg({ type: "err", text: errorText(err) }); }
setTesting(false);
};
const changePassword = async (e: React.FormEvent) => {
e.preventDefault();
try {
@@ -67,7 +97,6 @@ export default function SettingsPage() {
<div className="grid gap-6 lg:grid-cols-2">
<form onSubmit={save} className="card space-y-4">
<h2 className="font-semibold">Releases and alerts</h2>
{!smtp && <p className="rounded-lg bg-amber-50 p-3 text-sm text-amber-900">Email isn&apos;t set up on this server (SMTP_* in .env), so alerts are only logged.</p>}
{FIELDS.map(f => (
<div key={f.key}>
<label className="label" htmlFor={f.key}>{f.label}</label>
@@ -79,6 +108,35 @@ export default function SettingsPage() {
</form>
<div className="space-y-6">
<form onSubmit={saveSmtp} className="card space-y-3">
<h2 className="font-semibold">Email (SMTP)</h2>
<p className="text-sm text-slate-600">The mail server alert emails are sent through. The password is stored encrypted.</p>
{!smtp && <p className="rounded-lg bg-amber-50 p-3 text-sm text-amber-900">Email isn&apos;t set up, so alerts are only logged.</p>}
<div className="grid gap-3 sm:grid-cols-3">
<div className="sm:col-span-2"><label className="label" htmlFor="smtp_host">Server</label><input id="smtp_host" className="input" placeholder="mail.example.com" value={settings.smtp_host ?? ""} onChange={e => setSettings({ ...settings, smtp_host: e.target.value })} /></div>
<div><label className="label" htmlFor="smtp_port">Port</label><input id="smtp_port" className="input" type="number" value={settings.smtp_port ?? ""} onChange={e => setSettings({ ...settings, smtp_port: e.target.value })} /></div>
</div>
<p className="-mt-1 text-xs text-slate-500">587 uses STARTTLS; 465 uses TLS from the start.</p>
<div><label className="label" htmlFor="smtp_user">Username</label><input id="smtp_user" className="input" autoComplete="off" value={settings.smtp_user ?? ""} onChange={e => setSettings({ ...settings, smtp_user: e.target.value })} /></div>
<div>
<label className="label" htmlFor="smtp_pass">Password</label>
<input id="smtp_pass" className="input" type="password" autoComplete="new-password" disabled={smtpPass.clear} placeholder={settings.smtp_pass_set ? "Saved — leave blank to keep it" : ""} value={smtpPass.value} onChange={e => setSmtpPass({ ...smtpPass, value: e.target.value })} />
{settings.smtp_pass_set && (
<label className="mt-1 flex items-center gap-2 text-xs text-slate-600"><input type="checkbox" checked={smtpPass.clear} onChange={e => setSmtpPass({ value: "", clear: e.target.checked })} /> Remove the saved password</label>
)}
</div>
<div><label className="label" htmlFor="smtp_from">From address</label><input id="smtp_from" className="input" placeholder="Cross Events Hub <hub@crosscode.co.za>" value={settings.smtp_from ?? ""} onChange={e => setSettings({ ...settings, smtp_from: e.target.value })} /><p className="mt-1 text-xs text-slate-500">Blank uses the username.</p></div>
<button className="btn-primary">Save email settings</button>
<div className="border-t border-slate-100 pt-3">
<label className="label" htmlFor="smtp_test_to">Send a test email to</label>
<div className="flex flex-wrap gap-2">
<input id="smtp_test_to" className="input flex-1" type="email" placeholder={settings.alert_email || "you@example.com"} value={testTo} onChange={e => setTestTo(e.target.value)} />
<button type="button" className="btn-secondary" disabled={testing} onClick={testSmtp}>{testing ? "Testing…" : "Send test"}</button>
</div>
<p className="mt-1 text-xs text-slate-500">Tries what&apos;s in the form, saved or not. Blank sends to the alert email.</p>
</div>
</form>
<div className="card space-y-2">
<h2 className="font-semibold">Licence signing key</h2>
<p className="text-sm text-slate-600">Installs check licences with this public key (built into Cross Events: <code>backend/src/hub/publicKey.js</code>). The private key stays on this server only — keep a backup.</p>