Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

feat(email): add account setup and email verification templates

Tin8fe032230402
diff --git a/packages/email/src/components/security-reasons.ts b/packages/email/src/components/security-reasons.ts
index efa10f789..3d1e77333 100644
--- a/packages/email/src/components/security-reasons.ts
+++ b/packages/email/src/components/security-reasons.ts
@@ -27,3 +27,16 @@ export const passwordResetReason: Record<EmailLocale, Reason> = {
"zh-tw": (email, instance = "Kaneo") =>
`因為有人要求在 ${instance} 重設密碼,所以寄送至 ${email}。`,
};
+
+export const administratorReason: Record<EmailLocale, Reason> = {
+ en: (email, instance = "Kaneo") =>
+ `Sent to ${email} because an administrator of ${instance} asked Kaneo to send it.`,
+ de: (email, instance = "Kaneo") =>
+ `Gesendet an ${email}, weil eine Administratorin oder ein Administrator von ${instance} diese E-Mail angefordert hat.`,
+ vi: (email, instance = "Kaneo") =>
+ `Đã gửi đến ${email} vì quản trị viên của ${instance} đã yêu cầu Kaneo gửi email này.`,
+ ja: (email, instance = "Kaneo") =>
+ `${instance} の管理者がこのメールの送信をリクエストしたため、${email} に送信しました。`,
+ "zh-tw": (email, instance = "Kaneo") =>
+ `因為 ${instance} 的管理員要求 Kaneo 寄送這封郵件,所以寄送至 ${email}。`,
+};
diff --git a/packages/email/src/index.tsx b/packages/email/src/index.tsx
index a77bb0f12..4cb16e7e4 100644
--- a/packages/email/src/index.tsx
+++ b/packages/email/src/index.tsx
@@ -1,5 +1,8 @@
export { OTP_EXPIRY_SECONDS } from "./otp-expiry";
export {
+ type EmailResult,
+ sendAccountPasswordEmail,
+ sendEmailVerificationEmail,
sendMagicLinkEmail,
sendNotificationEmail,
sendOtpEmail,
@@ -14,3 +17,4 @@ export type {
TaskCardPriority,
} from "./components/task-card";
export type { NotificationEmailTask } from "./templates/notification";
+export type { AccountPasswordPurpose } from "./templates/account-password";
diff --git a/packages/email/src/send-email.tsx b/packages/email/src/send-email.tsx
index a8801362b..7fe1949a7 100644
--- a/packages/email/src/send-email.tsx
+++ b/packages/email/src/send-email.tsx
@@ -1,9 +1,16 @@
+import type { ReactElement } from "react";
import { render } from "react-email";
import { config } from "dotenv-mono";
import * as nodemailer from "nodemailer";
import { instanceHost } from "./components/instance-host";
import { getSmtpTransportOptions, isSmtpConfigured } from "./smtp-config";
import { getSmtpErrorDetails } from "./smtp-error-details";
+import AccountPasswordEmail, {
+ type AccountPasswordEmailProps,
+} from "./templates/account-password";
+import EmailVerificationEmail, {
+ type EmailVerificationEmailProps,
+} from "./templates/email-verification";
import type { MagicLinkEmailProps } from "./templates/magic-link";
import MagicLinkEmail from "./templates/magic-link";
import NotificationEmail, {
@@ -168,3 +175,55 @@ export const sendTrialReminderEmail = async (
console.error("Error sending trial reminder email", error);
}
};
+
+/**
+ * Emails an administrator asks Kaneo to send. They report delivery so the
+ * administrator can be told, and throw when the SMTP server rejects them.
+ */
+async function sendAdministratorEmail(
+ kind: string,
+ to: string,
+ subject: string,
+ template: ReactElement,
+): Promise<EmailResult> {
+ if (!isSmtpConfigured()) {
+ return { success: false, reason: "SMTP_NOT_CONFIGURED" };
+ }
+ try {
+ const html = await render(template);
+ await transporter.sendMail({
+ from: process.env.SMTP_FROM,
+ to,
+ subject,
+ html,
+ });
+ return { success: true };
+ } catch (error) {
+ console.error(`Error sending ${kind} email`, getSmtpErrorDetails(error));
+ throw new Error(`The ${kind} email could not be sent`);
+ }
+}
+
+export const sendAccountPasswordEmail = (
+ to: string,
+ subject: string,
+ data: AccountPasswordEmailProps,
+) =>
+ sendAdministratorEmail(
+ "account password",
+ to,
+ subject,
+ AccountPasswordEmail({ email: to, instance: instanceHost(), ...data }),
+ );
+
+export const sendEmailVerificationEmail = (
+ to: string,
+ subject: string,
+ data: EmailVerificationEmailProps,
+) =>
+ sendAdministratorEmail(
+ "email verification",
+ to,
+ subject,
+ EmailVerificationEmail({ email: to, instance: instanceHost(), ...data }),
+ );
diff --git a/packages/email/src/templates/account-password.test.ts b/packages/email/src/templates/account-password.test.ts
new file mode 100644
index 000000000..4cf557e5f
--- /dev/null
+++ b/packages/email/src/templates/account-password.test.ts
@@ -0,0 +1,37 @@
+import { render } from "@react-email/render";
+import { createElement } from "react";
+import { describe, expect, it } from "vite-plus/test";
+import AccountPasswordEmail from "./account-password";
+
+const link = "https://kaneo.example/api/auth/reset-password/abc";
+
+describe("AccountPasswordEmail", () => {
+ it("invites a new account to set a password instead of resetting one", async () => {
+ const html = await render(
+ createElement(AccountPasswordEmail, {
+ purpose: "setup",
+ passwordLink: link,
+ userName: "Maya",
+ email: "[email protected]",
+ instance: "kaneo.example",
+ }),
+ );
+ expect(html).toContain("Set up your Kaneo account");
+ expect(html).toContain("expires in 2 days");
+ expect(html).toContain("an administrator of kaneo.example");
+ expect(html).not.toContain("Reset");
+ expect(html).toContain(link);
+ });
+
+ it("explains an administrator-sent reset link in German", async () => {
+ const html = await render(
+ createElement(AccountPasswordEmail, {
+ purpose: "reset",
+ passwordLink: link,
+ locale: "de-DE",
+ }),
+ );
+ expect(html).toContain("Neues Passwort festlegen");
+ expect(html).toContain("1 Stunde");
+ });
+});
diff --git a/packages/email/src/templates/account-password.tsx b/packages/email/src/templates/account-password.tsx
new file mode 100644
index 000000000..5e979371c
--- /dev/null
+++ b/packages/email/src/templates/account-password.tsx
@@ -0,0 +1,201 @@
+import React from "react";
+import { EmailButton } from "../components/email-button";
+import { EmailFooter } from "../components/email-footer";
+import { EmailIntro } from "../components/email-intro";
+import { EmailNote } from "../components/email-note";
+import { EmailShell } from "../components/email-shell";
+import { FallbackLink } from "../components/fallback-link";
+import { footerLinks } from "../components/footer-links";
+import { administratorReason } from "../components/security-reasons";
+import { emailHtmlLang, resolveEmailLocale } from "./resolve-locale";
+
+void React;
+
+/**
+ * `setup`: an administrator created the account and the person chooses their
+ * first password. `reset`: an administrator sent a link to choose a new one.
+ */
+export type AccountPasswordPurpose = "setup" | "reset";
+
+export type AccountPasswordEmailProps = {
+ purpose: AccountPasswordPurpose;
+ passwordLink: string;
+ userName?: string;
+ locale?: string | null;
+ email?: string;
+ instance?: string;
+};
+
+type Copy = {
+ preview: string;
+ title: string;
+ subtitle: (name?: string) => string;
+ cta: string;
+ noteTitle: string;
+ note: string;
+};
+
+const fallback = {
+ en: "Button not working? Paste this link into your browser:",
+ de: "Schaltfläche funktioniert nicht? Füge diesen Link in deinen Browser ein:",
+ vi: "Nút không hoạt động? Dán liên kết này vào trình duyệt:",
+ ja: "ボタンが機能しない場合は、このリンクをブラウザに貼り付けてください:",
+ "zh-tw": "按鈕無法使用?請將此連結貼到瀏覽器:",
+} as const;
+
+const footer = {
+ en: "Kaneo account email",
+ de: "Kaneo Konto-E-Mail",
+ vi: "Email tài khoản Kaneo",
+ ja: "Kaneo アカウントメール",
+ "zh-tw": "Kaneo 帳號通知",
+} as const;
+
+const setup: Record<keyof typeof fallback, Copy> = {
+ en: {
+ preview:
+ "An administrator created a Kaneo account for you. Choose a password to sign in.",
+ title: "Set up your Kaneo account",
+ subtitle: (name) =>
+ `${name ? `Hi ${name}, an` : "An"} administrator created a Kaneo account for you. Choose a password to sign in. The link works once and expires in 2 days.`,
+ cta: "Set password",
+ noteTitle: "Not expecting this?",
+ note: "You can ignore this email. No password is set until someone opens this link and chooses one.",
+ },
+ de: {
+ preview:
+ "Eine Administratorin oder ein Administrator hat ein Kaneo-Konto für dich angelegt. Lege ein Passwort fest, um dich anzumelden.",
+ title: "Richte dein Kaneo-Konto ein",
+ subtitle: (name) =>
+ `${name ? `Hallo ${name}, eine` : "Eine"} Administratorin oder ein Administrator hat ein Kaneo-Konto für dich angelegt. Lege ein Passwort fest, um dich anzumelden. Der Link funktioniert nur einmal und läuft in 2 Tagen ab.`,
+ cta: "Passwort festlegen",
+ noteTitle: "Hast du das nicht erwartet?",
+ note: "Dann kannst du diese E-Mail ignorieren. Es wird kein Passwort festgelegt, bis jemand diesen Link öffnet und eines wählt.",
+ },
+ vi: {
+ preview:
+ "Quản trị viên đã tạo tài khoản Kaneo cho bạn. Hãy đặt mật khẩu để đăng nhập.",
+ title: "Thiết lập tài khoản Kaneo",
+ subtitle: (name) =>
+ `${name ? `Chào ${name}, quản` : "Quản"} trị viên đã tạo tài khoản Kaneo cho bạn. Hãy đặt mật khẩu để đăng nhập. Liên kết chỉ dùng được một lần và hết hạn sau 2 ngày.`,
+ cta: "Đặt mật khẩu",
+ noteTitle: "Bạn không mong đợi email này?",
+ note: "Bạn có thể bỏ qua email này. Sẽ không có mật khẩu nào được đặt cho đến khi có người mở liên kết này và chọn mật khẩu.",
+ },
+ ja: {
+ preview:
+ "管理者があなたの Kaneo アカウントを作成しました。パスワードを設定してサインインしてください。",
+ title: "Kaneo アカウントの設定",
+ subtitle: (name) =>
+ `${name ? `${name} さん、` : ""}管理者があなたの Kaneo アカウントを作成しました。パスワードを設定してサインインしてください。リンクは1回のみ有効で、2日で有効期限が切れます。`,
+ cta: "パスワードを設定",
+ noteTitle: "心当たりがありませんか?",
+ note: "このメールは無視してかまいません。誰かがこのリンクを開いてパスワードを設定しない限り、パスワードは設定されません。",
+ },
+ "zh-tw": {
+ preview: "管理員已為你建立 Kaneo 帳號。請設定密碼以登入。",
+ title: "設定你的 Kaneo 帳號",
+ subtitle: (name) =>
+ `${name ? `${name} 你好,` : ""}管理員已為你建立 Kaneo 帳號。請設定密碼以登入。連結只能使用一次,並將在 2 天後失效。`,
+ cta: "設定密碼",
+ noteTitle: "沒有預期收到這封郵件?",
+ note: "你可以忽略這封郵件。在有人開啟此連結並設定密碼之前,不會設定任何密碼。",
+ },
+};
+
+const reset: Record<keyof typeof fallback, Copy> = {
+ en: {
+ preview:
+ "An administrator sent you a link to choose a new password. It expires in 1 hour.",
+ title: "Choose a new password",
+ subtitle: (name) =>
+ `${name ? `Hi ${name}, an` : "An"} administrator sent you a link to choose a new Kaneo password. The link works once and expires in 1 hour.`,
+ cta: "Choose new password",
+ noteTitle: "Don't need a new password?",
+ note: "You can ignore this email. Your password stays the same until someone opens this link and sets a new one.",
+ },
+ de: {
+ preview:
+ "Eine Administratorin oder ein Administrator hat dir einen Link für ein neues Passwort geschickt. Er läuft in 1 Stunde ab.",
+ title: "Neues Passwort festlegen",
+ subtitle: (name) =>
+ `${name ? `Hallo ${name}, eine` : "Eine"} Administratorin oder ein Administrator hat dir einen Link geschickt, mit dem du ein neues Kaneo-Passwort festlegst. Der Link funktioniert nur einmal und läuft in 1 Stunde ab.`,
+ cta: "Neues Passwort festlegen",
+ noteTitle: "Brauchst du kein neues Passwort?",
+ note: "Dann kannst du diese E-Mail ignorieren. Dein Passwort bleibt gleich, bis jemand diesen Link öffnet und ein neues festlegt.",
+ },
+ vi: {
+ preview:
+ "Quản trị viên đã gửi cho bạn liên kết để đặt mật khẩu mới. Liên kết hết hạn sau 1 giờ.",
+ title: "Đặt mật khẩu mới",
+ subtitle: (name) =>
+ `${name ? `Chào ${name}, quản` : "Quản"} trị viên đã gửi cho bạn liên kết để đặt mật khẩu Kaneo mới. Liên kết chỉ dùng được một lần và hết hạn sau 1 giờ.`,
+ cta: "Đặt mật khẩu mới",
+ noteTitle: "Bạn không cần mật khẩu mới?",
+ note: "Bạn có thể bỏ qua email này. Mật khẩu của bạn sẽ giữ nguyên cho đến khi có người mở liên kết này và đặt mật khẩu mới.",
+ },
+ ja: {
+ preview:
+ "管理者から新しいパスワードを設定するためのリンクが届きました。有効期限は1時間です。",
+ title: "新しいパスワードの設定",
+ subtitle: (name) =>
+ `${name ? `${name} さん、` : ""}管理者から Kaneo の新しいパスワードを設定するためのリンクが届きました。リンクは1回のみ有効で、1時間で有効期限が切れます。`,
+ cta: "新しいパスワードを設定",
+ noteTitle: "新しいパスワードが不要な場合",
+ note: "このメールは無視してかまいません。誰かがこのリンクを開いて新しいパスワードを設定しない限り、パスワードは変更されません。",
+ },
+ "zh-tw": {
+ preview: "管理員寄給你設定新密碼的連結。連結將在 1 小時後失效。",
+ title: "設定新密碼",
+ subtitle: (name) =>
+ `${name ? `${name} 你好,` : ""}管理員寄給你設定新 Kaneo 密碼的連結。連結只能使用一次,並將在 1 小時後失效。`,
+ cta: "設定新密碼",
+ noteTitle: "不需要新密碼?",
+ note: "你可以忽略這封郵件。在有人開啟此連結並設定新密碼之前,你的密碼都不會變更。",
+ },
+};
+
+const AccountPasswordEmail = ({
+ purpose,
+ passwordLink,
+ userName,
+ locale,
+ email,
+ instance,
+}: AccountPasswordEmailProps) => {
+ const emailLocale = resolveEmailLocale(locale);
+ const copy = (purpose === "setup" ? setup : reset)[emailLocale];
+
+ return (
+ <EmailShell
+ title={copy.title}
+ preview={copy.preview}
+ lang={emailHtmlLang(emailLocale)}
+ >
+ <EmailIntro title={copy.title} subtitle={copy.subtitle(userName)} />
+ <EmailButton href={passwordLink} icon="key-round-light">
+ {copy.cta}
+ </EmailButton>
+ <FallbackLink label={fallback[emailLocale]} href={passwordLink} />
+ <EmailNote title={copy.noteTitle}>{copy.note}</EmailNote>
+ <EmailFooter
+ title={footer[emailLocale]}
+ reason={
+ email ? administratorReason[emailLocale](email, instance) : undefined
+ }
+ links={footerLinks(emailLocale)}
+ />
+ </EmailShell>
+ );
+};
+
+AccountPasswordEmail.PreviewProps = {
+ purpose: "setup",
+ passwordLink: "https://cloud.kaneo.app/auth/reset-password?token=example",
+ userName: "Maya",
+ locale: "en-US",
+ email: "[email protected]",
+ instance: "cloud.kaneo.app",
+} as AccountPasswordEmailProps;
+
+export default AccountPasswordEmail;
diff --git a/packages/email/src/templates/email-verification.test.ts b/packages/email/src/templates/email-verification.test.ts
new file mode 100644
index 000000000..9b9b5cc11
--- /dev/null
+++ b/packages/email/src/templates/email-verification.test.ts
@@ -0,0 +1,17 @@
+import { render } from "@react-email/render";
+import { createElement } from "react";
+import { describe, expect, it } from "vite-plus/test";
+import EmailVerificationEmail from "./email-verification";
+
+describe("EmailVerificationEmail", () => {
+ it("asks the person to confirm their address", async () => {
+ const html = await render(
+ createElement(EmailVerificationEmail, {
+ verificationLink: "https://kaneo.example/api/auth/verify-email?token=x",
+ locale: "ja-JP",
+ }),
+ );
+ expect(html).toContain("メールアドレスの確認");
+ expect(html).toContain("verify-email?token=x");
+ });
+});
diff --git a/packages/email/src/templates/email-verification.tsx b/packages/email/src/templates/email-verification.tsx
new file mode 100644
index 000000000..b7bcc586d
--- /dev/null
+++ b/packages/email/src/templates/email-verification.tsx
@@ -0,0 +1,124 @@
+import React from "react";
+import { EmailButton } from "../components/email-button";
+import { EmailFooter } from "../components/email-footer";
+import { EmailIntro } from "../components/email-intro";
+import { EmailNote } from "../components/email-note";
+import { EmailShell } from "../components/email-shell";
+import { FallbackLink } from "../components/fallback-link";
+import { footerLinks } from "../components/footer-links";
+import { administratorReason } from "../components/security-reasons";
+import { emailHtmlLang, resolveEmailLocale } from "./resolve-locale";
+
+void React;
+
+export type EmailVerificationEmailProps = {
+ verificationLink: string;
+ userName?: string;
+ locale?: string | null;
+ email?: string;
+ instance?: string;
+};
+
+const messages = {
+ en: {
+ preview: "Confirm that this email address belongs to you.",
+ title: "Confirm your email address",
+ subtitle: (name?: string) =>
+ `${name ? `Hi ${name}, please` : "Please"} confirm that this address belongs to you so it can be used with your Kaneo account. The link expires in 24 hours.`,
+ cta: "Confirm email",
+ fallback: "Button not working? Paste this link into your browser:",
+ noteTitle: "Not your account?",
+ note: "You can ignore this email. Nothing changes unless someone opens this link.",
+ footer: "Kaneo account email",
+ },
+ de: {
+ preview: "Bestätige, dass diese E-Mail-Adresse dir gehört.",
+ title: "Bestätige deine E-Mail-Adresse",
+ subtitle: (name?: string) =>
+ `${name ? `Hallo ${name}, bitte` : "Bitte"} bestätige, dass diese Adresse dir gehört, damit sie für dein Kaneo-Konto verwendet werden kann. Der Link läuft in 24 Stunden ab.`,
+ cta: "E-Mail bestätigen",
+ fallback:
+ "Schaltfläche funktioniert nicht? Füge diesen Link in deinen Browser ein:",
+ noteTitle: "Nicht dein Konto?",
+ note: "Dann kannst du diese E-Mail ignorieren. Es ändert sich nichts, solange niemand diesen Link öffnet.",
+ footer: "Kaneo Konto-E-Mail",
+ },
+ vi: {
+ preview: "Xác nhận địa chỉ email này thuộc về bạn.",
+ title: "Xác nhận địa chỉ email",
+ subtitle: (name?: string) =>
+ `${name ? `Chào ${name}, hãy` : "Hãy"} xác nhận địa chỉ này thuộc về bạn để có thể dùng với tài khoản Kaneo. Liên kết hết hạn sau 24 giờ.`,
+ cta: "Xác nhận email",
+ fallback: "Nút không hoạt động? Dán liên kết này vào trình duyệt:",
+ noteTitle: "Không phải tài khoản của bạn?",
+ note: "Bạn có thể bỏ qua email này. Sẽ không có gì thay đổi trừ khi có người mở liên kết này.",
+ footer: "Email tài khoản Kaneo",
+ },
+ ja: {
+ preview: "このメールアドレスがあなたのものであることを確認してください。",
+ title: "メールアドレスの確認",
+ subtitle: (name?: string) =>
+ `${name ? `${name} さん、` : ""}このアドレスを Kaneo アカウントで使用できるよう、あなたのものであることを確認してください。リンクは24時間で有効期限が切れます。`,
+ cta: "メールアドレスを確認",
+ fallback:
+ "ボタンが機能しない場合は、このリンクをブラウザに貼り付けてください:",
+ noteTitle: "心当たりがありませんか?",
+ note: "このメールは無視してかまいません。誰かがこのリンクを開かない限り、何も変更されません。",
+ footer: "Kaneo アカウントメール",
+ },
+ "zh-tw": {
+ preview: "確認這個電子郵件地址屬於你。",
+ title: "確認你的電子郵件地址",
+ subtitle: (name?: string) =>
+ `${name ? `${name} 你好,` : ""}請確認這個地址屬於你,以便用於你的 Kaneo 帳號。連結將在 24 小時後失效。`,
+ cta: "確認電子郵件",
+ fallback: "按鈕無法使用?請將此連結貼到瀏覽器:",
+ noteTitle: "不是你的帳號?",
+ note: "你可以忽略這封郵件。除非有人開啟此連結,否則不會有任何變更。",
+ footer: "Kaneo 帳號通知",
+ },
+} as const;
+
+const EmailVerificationEmail = ({
+ verificationLink,
+ userName,
+ locale,
+ email,
+ instance,
+}: EmailVerificationEmailProps) => {
+ const emailLocale = resolveEmailLocale(locale);
+ const copy = messages[emailLocale];
+
+ return (
+ <EmailShell
+ title={copy.title}
+ preview={copy.preview}
+ lang={emailHtmlLang(emailLocale)}
+ >
+ <EmailIntro title={copy.title} subtitle={copy.subtitle(userName)} />
+ <EmailButton href={verificationLink} icon="check-circle-light">
+ {copy.cta}
+ </EmailButton>
+ <FallbackLink label={copy.fallback} href={verificationLink} />
+ <EmailNote title={copy.noteTitle}>{copy.note}</EmailNote>
+ <EmailFooter
+ title={copy.footer}
+ reason={
+ email ? administratorReason[emailLocale](email, instance) : undefined
+ }
+ links={footerLinks(emailLocale)}
+ />
+ </EmailShell>
+ );
+};
+
+EmailVerificationEmail.PreviewProps = {
+ verificationLink:
+ "https://cloud.kaneo.app/api/auth/verify-email?token=example",
+ userName: "Maya",
+ locale: "en-US",
+ email: "[email protected]",
+ instance: "cloud.kaneo.app",
+} as EmailVerificationEmailProps;
+
+export default EmailVerificationEmail;