fix(auth): send magic links through Mailgun in prod, fail clearly without a mailer #25

Manually merged
jfro merged 0 commits from fix/10-prod-mailer into main 2026-11-01 17:48:02 +00:00
Owner

Closes #10 (gap audit F1).

In prod, config/prod.exs kept Swoosh.Adapters.Local with its storage turned off, so registering or "Log in with email" exited with :noproc, and without OIDC nobody could log in.

What changes

  • config/runtime.exs (prod): Swoosh's Mailgun adapter over the existing Req API client when MAILGUN_API_KEY and MAILGUN_DOMAIN are set (Mailgun per Jeremy, no new dependency). MAILGUN_BASE_URL for EU domains, MAIL_FROM as addr or Name <addr> (default lamplighter@<MAILGUN_DOMAIN>). This is the mailer #5's email channel can reuse.
  • Without those vars the adapter is the new Lamplighter.Mailer.Unconfigured, which returns {:error, :mailer_not_configured} instead of crashing.
  • UserNotifier uses Mailer.from/0 (was contact@example.com) and logs failed sends with subject, recipient domain, adapter, duration and reason.
  • Login: without a mailer the email-link form is hidden with a note; password and SSO login stay. The /dev/mailbox banner only shows with dev_routes.
  • Registration: without a mailer it says so and creates no account (it couldn't be confirmed). If a send fails after registering, it flashes an error instead of crashing. Changing email in settings does the same.
  • README: the new env vars, replacing "can't send email in production yet".

Tests: test/lamplighter/mailer_test.exs (7): reads runtime.exs for :prod with and without the Mailgun vars, MAIL_FROM parsing, the unconfigured send returns an error and logs it, and the login and registration pages without a mailer. 6 of 7 fail on main. mix precommit: 631 pass.

Not tried against a live Mailgun account.

Screenshots (no mailer configured), desktop and 390px:
10-login-desktop
10-register-desktop
10-login-mobile
10-register-mobile

Closes #10 (gap audit F1). In prod, `config/prod.exs` kept `Swoosh.Adapters.Local` with its storage turned off, so registering or "Log in with email" exited with `:noproc`, and without OIDC nobody could log in. **What changes** - `config/runtime.exs` (prod): Swoosh's Mailgun adapter over the existing Req API client when `MAILGUN_API_KEY` and `MAILGUN_DOMAIN` are set (Mailgun per Jeremy, no new dependency). `MAILGUN_BASE_URL` for EU domains, `MAIL_FROM` as `addr` or `Name <addr>` (default `lamplighter@<MAILGUN_DOMAIN>`). This is the mailer #5's email channel can reuse. - Without those vars the adapter is the new `Lamplighter.Mailer.Unconfigured`, which returns `{:error, :mailer_not_configured}` instead of crashing. - `UserNotifier` uses `Mailer.from/0` (was `contact@example.com`) and logs failed sends with subject, recipient domain, adapter, duration and reason. - Login: without a mailer the email-link form is hidden with a note; password and SSO login stay. The `/dev/mailbox` banner only shows with `dev_routes`. - Registration: without a mailer it says so and creates no account (it couldn't be confirmed). If a send fails after registering, it flashes an error instead of crashing. Changing email in settings does the same. - README: the new env vars, replacing "can't send email in production yet". **Tests**: `test/lamplighter/mailer_test.exs` (7): reads `runtime.exs` for `:prod` with and without the Mailgun vars, MAIL_FROM parsing, the unconfigured send returns an error and logs it, and the login and registration pages without a mailer. 6 of 7 fail on main. `mix precommit`: 631 pass. Not tried against a live Mailgun account. **Screenshots** (no mailer configured), desktop and 390px: ![10-login-desktop](https://git.knope.dev/attachments/a0d14d9c-aaf9-4fb6-a89a-264a3240c423) ![10-register-desktop](https://git.knope.dev/attachments/430bf26c-d8ec-4ae1-91b8-91b45aee402e) ![10-login-mobile](https://git.knope.dev/attachments/a0735263-e01e-4bc8-a02e-302c6c9a5564) ![10-register-mobile](https://git.knope.dev/attachments/e889aa07-8542-4d40-8f79-bbb3a3b267d5)
jfro added this to the v1.0 milestone 2026-11-01 16:59:09 +00:00
fix(auth): send magic links through Mailgun in prod, and fail clearly without a mailer
All checks were successful
CI / test (pull_request) Successful in 23s
38025f80a5
prod.exs kept the Local adapter with its storage off, so registering or
asking for a login link exited with :noproc and nobody without OIDC could
log in. runtime.exs now configures Swoosh's Mailgun adapter (over Req)
from MAILGUN_API_KEY/MAILGUN_DOMAIN (MAILGUN_BASE_URL for EU, MAIL_FROM
for the sender), and otherwise Lamplighter.Mailer.Unconfigured, which
returns an error. Failed sends are logged with the adapter and duration;
the login page hides the email link form and registration refuses to
create accounts it can't confirm. The /dev/mailbox banner only shows
with dev routes.

Closes #10
Author
Owner

Review (Nye) at 38025f8: approve.

Checked against Swoosh 1.28.1's Mailgun adapter and Swoosh.ApiClient.Req:

  • mix precommit passes (632 tests incl. mailer_test.exs). CI green.
  • Extra test (test/review/mailgun_deliver_test.exs, attached): with adapter Mailgun + Swoosh.ApiClient.Req and Mailgun stubbed via Req.Test, a magic link POSTs form-encoded to /v3//messages and returns {:ok, %{id: _}}.
  • Runtime config, MAIL_FROM parsing, the Unconfigured adapter, the login/registration/settings error paths, and logging (recipient domain only, no address) all look right. The dev mailbox banner is gated on dev_routes.

Note, not blocking: Swoosh's Mailgun adapter declares required_deps: [multipart: Multipart, ...], and Lamplighter.Mailer has @on_load :validate_dependency. If the Mailer module is first loaded after the adapter is Mailgun, the on_load aborts and the module won't load (seen in test, where modules load lazily). In a release, modules load at boot before runtime.exs runs (checked with a scratch release: on_load saw the build-time adapter), so bin/server is fine. bin/... eval paths that send mail, and any attachment, would fail. Adding {:multipart, "~> 0.4"} would make this robust; fine as a follow-up.

Review (Nye) at 38025f8: approve. Checked against Swoosh 1.28.1's Mailgun adapter and Swoosh.ApiClient.Req: - `mix precommit` passes (632 tests incl. mailer_test.exs). CI green. - Extra test (test/review/mailgun_deliver_test.exs, attached): with adapter Mailgun + Swoosh.ApiClient.Req and Mailgun stubbed via Req.Test, a magic link POSTs form-encoded to /v3/<domain>/messages and returns {:ok, %{id: _}}. - Runtime config, MAIL_FROM parsing, the Unconfigured adapter, the login/registration/settings error paths, and logging (recipient domain only, no address) all look right. The dev mailbox banner is gated on dev_routes. Note, not blocking: Swoosh's Mailgun adapter declares `required_deps: [multipart: Multipart, ...]`, and `Lamplighter.Mailer` has `@on_load :validate_dependency`. If the Mailer module is first loaded *after* the adapter is Mailgun, the on_load aborts and the module won't load (seen in test, where modules load lazily). In a release, modules load at boot before runtime.exs runs (checked with a scratch release: on_load saw the build-time adapter), so `bin/server` is fine. `bin/... eval` paths that send mail, and any attachment, would fail. Adding `{:multipart, "~> 0.4"}` would make this robust; fine as a follow-up.
jfro manually merged commit 5ef110e656 into main 2026-11-01 17:48:02 +00:00
Sign in to join this conversation.
No description provided.