import { EmailConfig } from "next-auth/providers/email"; import { getTranslations } from "next-intl/server"; import { createTransport } from "nodemailer"; import type { Transport, TransportOptions } from "nodemailer"; import SMTPTransport from "nodemailer/lib/smtp-transport"; import SMTPPool from "nodemailer/lib/smtp-pool"; import SendmailTransport from "nodemailer/lib/sendmail-transport"; import StreamTransport from "nodemailer/lib/stream-transport"; import JSONTransport from "nodemailer/lib/json-transport"; import SESTransport from "nodemailer/lib/ses-transport"; type Awaitable = T | PromiseLike; function html(params: { url: string; host: string; t: Awaited>>; }) { const { url, host, t } = params; const escapedHost = host.replace(/\./g, "​."); const brandColor = "#346df1"; const buttonText = "#fff"; const color = { background: "#f9f9f9", text: "#444", mainBackground: "#fff", buttonBackground: brandColor, buttonBorder: brandColor, buttonText, }; return `
${t("email.subject", { host: escapedHost })}
${t("email.button")}
${t("email.ignore")}
`; } /** Email Text body (fallback for email clients that don't render HTML, e.g. feature phones) */ function text({ url, host, t, }: { url: string; host: string; t: Awaited>>; }) { return t("email.textMessage", { host, url }); } type AllTransportOptions = | string | SMTPTransport | SMTPTransport.Options | SMTPPool | SMTPPool.Options | SendmailTransport | SendmailTransport.Options | StreamTransport | StreamTransport.Options | JSONTransport | JSONTransport.Options | SESTransport | SESTransport.Options | Transport | TransportOptions; export interface NodemailerConfig extends EmailConfig { server?: AllTransportOptions; sendVerificationRequest: (params: { identifier: string; url: string; expires: Date; provider: NodemailerConfig; token: string; theme: any; request: Request; }) => Awaitable; options?: NodemailerUserConfig; } export type NodemailerUserConfig = Omit, "options" | "type">; export default function Nodemailer(config: NodemailerUserConfig): NodemailerConfig { return { id: "nodemailer", type: "email", name: "Nodemailer", server: { host: "localhost", port: 25, auth: { user: "", pass: "" } }, from: "Echecs France ", maxAge: 24 * 60 * 60, async sendVerificationRequest(params) { const { identifier, url, provider } = params; let locale = "fr"; const { searchParams } = new URL(url); if (searchParams.has("callbackUrl")) { const { pathname } = new URL(searchParams.get("callbackUrl")!); const maybeLocale = pathname.split("/")[1]; if (["en", "fr"].includes(maybeLocale)) { locale = maybeLocale; } } const t = await getTranslations({ locale, namespace: "SignIn" }); const { host } = new URL(url); if (!(config.server as unknown as any)?.host) { console.log("Connection URL", url); console.log(text({ url, host, t })); return; } const transport = createTransport(provider.server); const result = await transport.sendMail({ to: identifier, from: provider.from, subject: t("email.subject", { host }), text: text({ url, host, t }), html: html({ url, host, t }), }); const failed = result.rejected.concat(result.pending).filter(Boolean); if (failed.length) { throw new Error(`Email (${failed.join(", ")}) could not be sent`); } }, options: config, }; }