Files
loki/scripts/site-template/lib/mail.ts
CatoandClaude c83c8f473e feat(starter): kickoff and new-site.sh both produce the fleet starter (#1138)
scripts/site-template is now bitbaum/fleet templates/app — Next.js 16,
better-auth accounts (password, link, passkey, 2FA, OrangeCat/bitbaum sign-in
as env-gated OIDC), en/de/fr, nonce CSP, ai-kit/mail-kit/limitkit/bip-kit,
tests and CI — copied at a pinned fleet commit by
scripts/templates/sync-starter.sh. CI proves the copy is exact
(sync-starter.sh --check), and starter-sync.yml opens the PR when fleet's
starter moves. Never edited here.

The kickoff's "nextjs-tailwind" starter seeds the same app from
src/lib/starter-app.generated.ts (generated from the directory, pinned by
scripts/test/starter-app.ts), minus the workflows register-site.sh adds with
the site's CD. The repo is seeded in one Trees API call with inline contents
instead of a blob POST per file (~85 for this starter). The widget token rides
in .env.production; the starter's .gitleaks.toml allows NEXT_PUBLIC_* values.
The new-from-scratch picker reads the light template list, so the starter's
file bodies never reach a client bundle.

Both paths give the site its database and auth/seal secrets ON THE BOX
(scripts/hetzner/provision-db-on-box.sh: role + database, ~/.db-credentials,
/opt/<slug>/shared/.env; existing secrets never replaced; nothing printed), and
record the db in the register row so every deploy applies drizzle/.
register-site.sh seeds the starter's full workflow set to starter repos and
the golden CI floor to anything else, never overwriting another repo's ci.yml.

test-site-template.sh follows the starter (verify SSOT, design tokens instead
of a local palette) and its comment stripper is fixed: the sed range deleted
the page from the doc comment to EOF, so every day-zero check had been passing
against an empty string. check-template-contrast.mjs and the starter-lock
generator are retired with the files they checked.


Claude-Session: https://claude.ai/code/session_01W7K2Gm5wcbfCi7THtmV1Hy

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-11 13:39:14 -07:00

87 lines
3.3 KiB
TypeScript

import { isMailConfigured, sendMail } from "@bitbaum/mail-kit";
import { createTranslator } from "next-intl";
import de from "@/messages/de.json";
import en from "@/messages/en.json";
import fr from "@/messages/fr.json";
import { withFallback } from "@/i18n/request";
import { defaultLocale, toLocale, type Locale } from "@/i18n/routing";
import { isDeliverable } from "@/lib/auth/methods";
import { SITE } from "@/lib/site";
/**
* The account mails: confirm an address, sign in by link, reset a password.
* Sent through @bitbaum/mail-kit (one provider, one env contract). Never
* throws, and never logs who it was for or what it carried — a sign-in link in
* a log is a credential anyone with log access can use.
*/
export type AuthMail = "verify" | "magicLink" | "reset";
type Messages = typeof en;
const MESSAGES: Record<Locale, Messages> = {
en,
de: withFallback(en, de) as Messages,
fr: withFallback(en, fr) as Messages,
};
export function mailConfigured(): boolean {
return isMailConfigured();
}
/**
* The language a mail is written in, read from the page the person will land
* on. better-auth's links carry a `callbackURL`; ours always start with the
* locale (`/de/account`), so the mail speaks the language they were reading.
*/
export function localeOfLink(url: string): Locale {
try {
const parsed = new URL(url);
const callback = parsed.searchParams.get("callbackURL") ?? parsed.pathname;
const first = callback.replace(/^https?:\/\/[^/]+/, "").split("/")[1] ?? "";
return toLocale(first);
} catch {
return defaultLocale;
}
}
const escapeHtml = (value: string) =>
value.replace(
/[&<>"']/g,
(c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[c] ?? c,
);
export function renderAuthMail(kind: AuthMail, url: string, locale: Locale) {
const t = createTranslator({ locale, messages: MESSAGES[locale], namespace: "Mail" });
const values = { site: SITE.title };
const subject = t(`${kind}.subject`, values);
const lead = t(`${kind}.lead`, values);
const action = t(`${kind}.action`);
const ignore = t("ignore");
const href = escapeHtml(url);
const html = `<!doctype html><html lang="${locale}"><body style="font-family:system-ui,sans-serif;line-height:1.5">
<p>${escapeHtml(lead)}</p>
<p><a href="${href}">${escapeHtml(action)}</a></p>
<p style="color:#555">${escapeHtml(ignore)}</p>
</body></html>`;
const textBody = `${lead}\n\n${action}: ${url}\n\n${ignore}\n`;
return { subject, html, text: textBody };
}
export async function sendAuthMail(kind: AuthMail, to: string, url: string): Promise<void> {
if (!isDeliverable(to)) return;
if (!isMailConfigured()) {
// Development only: there is no inbox, so the link goes to the console of
// the person running the server. Production never reaches this line with
// a link, because the methods that need mail are hidden without it.
if (process.env.NODE_ENV === "development")
console.warn(`[mail] not configured — ${kind}: ${url}`);
return;
}
const { subject, html, text } = renderAuthMail(kind, url, localeOfLink(url));
const result = await sendMail({ to, subject, html, text });
if (!result.sent) {
console.error(
`[mail] ${kind} not sent (status ${result.status ?? "none"}, retryable ${result.retryable})`,
);
}
}