Skip to content

feat: localized block-based mail templating via ilingo & @authup/i18n - #3109

Merged
tada5hi merged 1 commit into
masterfrom
feat/mail-template-engine
Jun 11, 2026
Merged

tada5hi merged 1 commit into
masterfrom
feat/mail-template-engine

Conversation

@tada5hi

@tada5hi tada5hi commented Jun 11, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Refactors and strengthens the email templating mechanism for the identity workflows (registration activation, password reset). Previously: mail copy was a hand-rolled per-locale record in core/mail/templates.ts with its own locale narrowing + interpolation, a single hardcoded HTML shell, and — although four locales were authored — no caller ever passed a locale, so every recipient got English.

Copy → @authup/i18n / ilingo

  • New authupMail namespace (TranslatorTranslationMailKey, catalogs/{en,de,fr,es}/mail.ts) with ilingo {{var}} placeholders (e.g. passwordResetExpiry → {{minutes}}).
  • The existing locale-parity test covers the namespace automatically; NamespaceTranslations<K> makes a missing key a compile error.
  • server-core resolves copy through Ilingo + MemoryStore({ data: CATALOGS }) (new runtime deps: @authup/i18n, ilingo). A missing key throws InternalError instead of rendering a hole.

Structure → typed block model (core/mail/format + core/mail/template)

  • Templates compose paragraph / code / action / note blocks via defineMailTemplate; the html and text multipart bodies derive from the same block list, so they cannot drift.
  • Centralized html escaping, <html lang> from the resolved locale, hidden preheader for mail-client preview lines.
  • render() / build() are async so a file-backed (FSStore) or remote override store can slot in later without an interface change (planned follow-up: operator-overridable templates from disk).

Locale threading (localization actually works now)

  • useRequestLocale(event): vc-locale cookie when it maps onto an authored locale (auto/unsupported fall through) → first supported language in routup's q-ordered getRequestAcceptableLanguages (pt-BR, de;q=0.8 → de).
  • Register / password-forgot controllers pass it via IdentityWorkflowContext into the services.

Hardening

  • Call-to-action URLs are centrally dropped unless http(s) — javascript: defense in depth.
  • PASSWORD_RESET_EXPIRES_IN_MINUTES drives both the persisted reset_expires and a localized "expires in 30 minutes" note; both mails gained a "you can safely ignore this email" hint.
  • MailTemplateRendererInjectionKey registered by MailModule (singleton), resolved by controller factories — a custom renderer is a registration swap.

Tests

  • packages/i18n: 77 passed (parity incl. authupMail).
  • apps/server-core: full suite 859 passed / 87 files; new per-locale render smoke matrix (templates.spec.ts), renderer hardening specs (escaping, URL guard, lang attr, preheader, expiry), service-level locale-flow specs.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Registration and password reset emails now adapt to user's preferred language based on browser settings
    • Password reset emails now clearly display how long the reset code remains valid

Refactor the email templating mechanism for the identity workflows
(registration activation, password reset):

- @authup/i18n: new `authupMail` namespace (TranslatorTranslationMailKey,
  catalogs/{en,de,fr,es}/mail.ts) holding transactional mail copy with
  ilingo `{{var}}` placeholders; covered by the locale-parity test.
- server-core mail engine: typed block model (paragraph/code/action/note)
  in core/mail/format — html and text multipart bodies derive from the
  same block list with centralized escaping, <html lang>, and a hidden
  preheader. Templates (core/mail/template) compose blocks and resolve
  copy per locale through Ilingo + MemoryStore(CATALOGS); async render()
  so file-/remote-backed override stores can slot in later.
- locale threading: useRequestLocale() resolves the first authored locale
  from the vc-locale cookie or routup's q-ordered Accept-Language chain;
  controllers pass it via IdentityWorkflowContext into the services.
- hardening: action URLs are dropped unless http(s) (javascript: defense
  in depth); PASSWORD_RESET_EXPIRES_IN_MINUTES drives both reset_expires
  and a localized expiry note; missing translations throw InternalError.
- wiring: MailTemplateRendererInjectionKey registered by MailModule
  (singleton) and resolved by the controller factories.
@coderabbitai

coderabbitai Bot commented Jun 11, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f6502687-44f6-4584-9078-511beb12cb2c

📥 Commits

Reviewing files that changed from the base of the PR and between a34530a and 88d5751.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (51)
  • .agents/architecture.md
  • .agents/structure.md
  • DEPENDENCY_GRAPH.md
  • apps/server-core/package.json
  • apps/server-core/src/adapters/http/controllers/workflows/password-forgot/module.ts
  • apps/server-core/src/adapters/http/controllers/workflows/register/module.ts
  • apps/server-core/src/adapters/http/request/helpers/index.ts
  • apps/server-core/src/adapters/http/request/helpers/locale.ts
  • apps/server-core/src/adapters/http/ui/render.ts
  • apps/server-core/src/app/modules/http/modules/controller.ts
  • apps/server-core/src/app/modules/mail/constants.ts
  • apps/server-core/src/app/modules/mail/module.ts
  • apps/server-core/src/core/identity/index.ts
  • apps/server-core/src/core/identity/password-recovery/constants.ts
  • apps/server-core/src/core/identity/password-recovery/index.ts
  • apps/server-core/src/core/identity/password-recovery/service.ts
  • apps/server-core/src/core/identity/password-recovery/types.ts
  • apps/server-core/src/core/identity/registration/service.ts
  • apps/server-core/src/core/identity/registration/types.ts
  • apps/server-core/src/core/identity/types.ts
  • apps/server-core/src/core/mail/format/blocks.ts
  • apps/server-core/src/core/mail/format/html.ts
  • apps/server-core/src/core/mail/format/index.ts
  • apps/server-core/src/core/mail/format/text.ts
  • apps/server-core/src/core/mail/format/types.ts
  • apps/server-core/src/core/mail/index.ts
  • apps/server-core/src/core/mail/renderer.ts
  • apps/server-core/src/core/mail/template/define.ts
  • apps/server-core/src/core/mail/template/index.ts
  • apps/server-core/src/core/mail/template/registry.ts
  • apps/server-core/src/core/mail/template/templates/password-reset.ts
  • apps/server-core/src/core/mail/template/templates/registration-activation.ts
  • apps/server-core/src/core/mail/template/types.ts
  • apps/server-core/src/core/mail/templates.ts
  • apps/server-core/src/core/mail/types.ts
  • apps/server-core/test/unit/core/identity/password-recovery/service.spec.ts
  • apps/server-core/test/unit/core/identity/registration/service.spec.ts
  • apps/server-core/test/unit/core/mail/renderer.spec.ts
  • apps/server-core/test/unit/core/mail/templates.spec.ts
  • apps/server-core/tsconfig.json
  • packages/i18n/src/catalogs/de/index.ts
  • packages/i18n/src/catalogs/de/mail.ts
  • packages/i18n/src/catalogs/en/index.ts
  • packages/i18n/src/catalogs/en/mail.ts
  • packages/i18n/src/catalogs/es/index.ts
  • packages/i18n/src/catalogs/es/mail.ts
  • packages/i18n/src/catalogs/fr/index.ts
  • packages/i18n/src/catalogs/fr/mail.ts
  • packages/i18n/src/catalogs/index.ts
  • packages/i18n/src/constants.ts
  • packages/i18n/test/unit/parity.spec.ts
💤 Files with no reviewable changes (1)
  • apps/server-core/src/core/mail/templates.ts

📝 Walkthrough

Walkthrough

This PR implements a localized mail template system with block-based document rendering, i18n integration, and dependency injection wiring. Request locale is extracted from cookies or Accept-Language headers and threaded through registration and password recovery workflows. Mail templates are refactored from simple strings to typed, composable blocks that render to both HTML (with styling and XSS escaping) and plain text. The system adds a new authupMail i18n namespace with translations for English, German, French, and Spanish covering activation and password reset flows.

Changes

Localized Mail Template System

Layer / File(s) Summary
Request locale resolution and identity workflow context
apps/server-core/src/adapters/http/request/helpers/locale.ts, apps/server-core/src/adapters/http/ui/render.ts, apps/server-core/src/core/identity/types.ts
Request locale is extracted by useRequestLocale using cookie-first fallback to Accept-Language header negotiation. IdentityWorkflowContext type carries optional locale through service boundaries for outbound email localization.
Mail document structure and block types
apps/server-core/src/core/mail/format/types.ts, apps/server-core/src/core/mail/format/blocks.ts, apps/server-core/src/core/mail/format/index.ts
Typed mail blocks (paragraph, code, action, note) are composable document pieces. Factory functions and isSafeActionURL validate HTTP(S)-only action links. Documents carry optional lang and preview metadata alongside a blocks array.
HTML and text mail rendering
apps/server-core/src/core/mail/format/html.ts, apps/server-core/src/core/mail/format/text.ts
HTML rendering escapes all interpolated content, applies inline styling, includes optional preheader, and wraps in a branded container. Text rendering formats blocks into plain-text with separators, filtering empty results.
Mail template system
apps/server-core/src/core/mail/template/types.ts, apps/server-core/src/core/mail/template/define.ts, apps/server-core/src/core/mail/template/index.ts
MailTemplateName enum and MailTemplateParamsMap define supported templates and their parameters. defineMailTemplate identity function preserves per-template type safety. Templates are async builders that accept a translator and return documents with subject, preview, and blocks.
Mail template registry and implementations
apps/server-core/src/core/mail/template/registry.ts, apps/server-core/src/core/mail/template/templates/password-reset.ts, apps/server-core/src/core/mail/template/templates/registration-activation.ts
Registry maps MailTemplateName keys to concrete template objects. Password reset template conditionally includes expiry note when params.expiresInMinutes is provided. Registration activation includes optional action block when URL is present. Both fetch localized strings on-demand through the translator.
i18n mail namespace and translations
packages/i18n/src/constants.ts, packages/i18n/src/catalogs/en/mail.ts, packages/i18n/src/catalogs/de/mail.ts, packages/i18n/src/catalogs/fr/mail.ts, packages/i18n/src/catalogs/es/mail.ts, packages/i18n/src/catalogs/index.ts
New authupMail namespace registered with TranslatorTranslationMailKey enum defining mail translation keys. Catalogs for EN, DE, FR, ES provide localized subject lines, introductions, CTAs, expiry notes, and hints with {{minutes}} interpolation placeholders for password resets.
MailTemplateRenderer refactor
apps/server-core/src/core/mail/renderer.ts, apps/server-core/src/core/mail/types.ts, apps/server-core/src/core/mail/index.ts
Renderer is rewritten as async. It uses Ilingo for locale-matched translation, MAIL_TEMPLATE_REGISTRY for template lookup, and renderHtmlDocument/renderTextDocument for output. Unsafe action blocks are filtered via isSafeActionURL. The interface makes render return Promise<MailContent> and the old string-based template module is removed.
Password recovery with expiry constant and locale
apps/server-core/src/core/identity/password-recovery/constants.ts, apps/server-core/src/core/identity/password-recovery/service.ts, apps/server-core/src/core/identity/password-recovery/types.ts
PASSWORD_RESET_EXPIRES_IN_MINUTES constant (30 min) drives both code expiry persistence and the email note. PasswordRecoveryService.forgotPassword accepts optional IdentityWorkflowContext and passes locale and expiresInMinutes to reset email template rendering.
Registration service with locale support
apps/server-core/src/core/identity/registration/service.ts, apps/server-core/src/core/identity/registration/types.ts
RegistrationService.register accepts optional IdentityWorkflowContext. When email verification is enabled, the activation email receives locale from context for localized subject and body rendering.
DI container wiring
apps/server-core/src/app/modules/mail/constants.ts, apps/server-core/src/app/modules/mail/module.ts, apps/server-core/src/app/modules/http/modules/controller.ts
MailTemplateRendererInjectionKey is defined as a TypedToken. MailModule.setup registers the renderer in the container. HTTPControllerModule resolves the renderer from the container (container.resolve(MailTemplateRendererInjectionKey)) instead of direct instantiation.
HTTP controller locale threading
apps/server-core/src/adapters/http/controllers/workflows/password-forgot/module.ts, apps/server-core/src/adapters/http/controllers/workflows/register/module.ts
Controllers import useRequestLocale and pass { locale: useRequestLocale(event) } as context when calling forgotPassword and register service methods, enabling request-scoped locale-aware email rendering.
Unit tests for localization, security, and rendering
apps/server-core/test/unit/core/identity/password-recovery/service.spec.ts, apps/server-core/test/unit/core/identity/registration/service.spec.ts, apps/server-core/test/unit/core/mail/renderer.spec.ts, apps/server-core/test/unit/core/mail/templates.spec.ts, packages/i18n/test/unit/parity.spec.ts
Tests verify async rendering, localized subject/body output, HTML escaping for interpolation, XSS prevention (unsafe URL filtering, preheader presence), and cross-locale smoke rendering. i18n parity test ensures all MAIL namespace keys are consistently translated.
Configuration and documentation updates
apps/server-core/package.json, apps/server-core/tsconfig.json, .agents/architecture.md, .agents/structure.md, DEPENDENCY_GRAPH.md
Dependencies include @authup/i18n and updated ilingo. TypeScript paths map @authup/i18n to the i18n package. Architecture docs explain the three-layer mail rendering model (i18n copy → typed blocks → safe rendering). Structure docs and dependency graph updated to reflect i18n and theme library integration.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • authup/authup#2945: Both PRs refactor the DI wiring in MailModule and HTTPControllerModule for container-based dependency resolution; this PR adds MailTemplateRendererInjectionKey registration and resolution in the same modules.

Poem

🐰 The mail now speaks in tongue and locale,
With blocks and safety checks that never fail,
From request's cookie to Ilingo's keep,
These templates render secrets safe and deep.
Five languages bloom where one was bare,
A rabbit's gift: localized care! 🌍✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 38.46% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: introducing localized, block-based mail templating powered by ilingo and the @authup/i18n package.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/mail-template-engine

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new

pkg-pr-new Bot commented Jun 11, 2026

Copy link
Copy Markdown

Open in StackBlitz

authup

npm i https://pkg.pr.new/authup@3109

@authup/client-web

npm i https://pkg.pr.new/@authup/client-web@3109

@authup/server-core

npm i https://pkg.pr.new/@authup/server-core@3109

@authup/access

npm i https://pkg.pr.new/@authup/access@3109

@authup/client-web-kit

npm i https://pkg.pr.new/@authup/client-web-kit@3109

@authup/client-web-kit-theme

npm i https://pkg.pr.new/@authup/client-web-kit-theme@3109

@authup/client-web-nuxt

npm i https://pkg.pr.new/@authup/client-web-nuxt@3109

@authup/client-web-theme

npm i https://pkg.pr.new/@authup/client-web-theme@3109

@authup/core-http-kit

npm i https://pkg.pr.new/@authup/core-http-kit@3109

@authup/core-kit

npm i https://pkg.pr.new/@authup/core-kit@3109

@authup/core-realtime-kit

npm i https://pkg.pr.new/@authup/core-realtime-kit@3109

@authup/errors

npm i https://pkg.pr.new/@authup/errors@3109

@authup/i18n

npm i https://pkg.pr.new/@authup/i18n@3109

@authup/kit

npm i https://pkg.pr.new/@authup/kit@3109

@authup/server-adapter-kit

npm i https://pkg.pr.new/@authup/server-adapter-kit@3109

@authup/server-adapter-node

npm i https://pkg.pr.new/@authup/server-adapter-node@3109

@authup/server-adapter-socket-io

npm i https://pkg.pr.new/@authup/server-adapter-socket-io@3109

@authup/server-adapter-web

npm i https://pkg.pr.new/@authup/server-adapter-web@3109

@authup/server-kit

npm i https://pkg.pr.new/@authup/server-kit@3109

@authup/server-test-kit

npm i https://pkg.pr.new/@authup/server-test-kit@3109

@authup/specs

npm i https://pkg.pr.new/@authup/specs@3109

commit: 88d5751

@tada5hi
tada5hi marked this pull request as ready for review June 11, 2026 10:51
Copilot AI review requested due to automatic review settings June 11, 2026 10:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@tada5hi
tada5hi merged commit 69c776e into master Jun 11, 2026
12 checks passed
@tada5hi
tada5hi deleted the feat/mail-template-engine branch June 11, 2026 11:43
@github-actions github-actions Bot mentioned this pull request Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants