import { Body, Container, Head, Heading, Hr, Html, Img, Link, Preview, pixelBasedPreset, Section, Tailwind, Text } from "@react-email/components" import type { ReactNode } from "react" import { cn } from "../../../lib/utils" import { type EmailClassNames, type EmailColors, EmailStyles } from "./email-styles" const otpEmailLocalization = { YOUR_VERIFICATION_CODE_IS_CODE: "Your verification code is {verificationCode}", LOGO: "Logo", VERIFY_YOUR_EMAIL: "Verify your email", WE_NEED_TO_VERIFY_YOUR_EMAIL_ADDRESS: "We need to verify your email address {email} before you can access your {appName} account. Enter the code below in your open browser window.", THIS_CODE_EXPIRES_IN_MINUTES: "This code expires in {expirationMinutes} minutes.", EMAIL_SENT_BY: "Email sent by {appName}.", IF_YOU_DIDNT_REQUEST_THIS_EMAIL: "If you didn't request this email, you can safely ignore it. Someone else might have typed your email address by mistake.", POWERED_BY_BETTER_AUTH: "Powered by {betterAuth}" } /** * Localization strings for the OtpEmail component. * * Contains all text content used in the OTP (One-Time Password) email template. */ export type OtpEmailEmailLocalization = typeof otpEmailLocalization /** * Props for the OtpEmail component. */ export interface OtpEmailProps { /** The one-time verification code to display */ verificationCode: string /** Email address being verified */ email?: string /** Name of the application sending the email */ appName?: string /** Number of minutes until the verification code expires */ expirationMinutes?: number /** Logo URL(s) - a single string or light/dark variants. If omitted, no logo is shown. */ logoURL?: string | { light: string; dark: string } /** Custom CSS class names for styling specific parts of the email */ classNames?: EmailClassNames /** Custom color scheme for light and dark modes */ colors?: EmailColors /** Whether to show the "Powered by better-auth" footer */ poweredBy?: boolean /** Whether to enable dark mode support */ darkMode?: boolean /** Additional React nodes to inject into the email head */ head?: ReactNode /** * Localization overrides for customizing email text * @remarks `OtpEmailEmailLocalization` */ localization?: Partial } /** * Email template component that sends one-time password (OTP) verification codes to users. * * This email includes: * - Large, prominently displayed verification code * - Expiration time information * - Security notice for unauthorized requests * - Customizable branding and styling * - Support for light/dark mode themes * * @example * ```tsx * * ``` */ export const OtpEmail = ({ verificationCode, email, appName, expirationMinutes = 10, logoURL, colors, classNames, darkMode = true, poweredBy, head, ...props }: OtpEmailProps) => { const localization = { ...OtpEmail.localization, ...props.localization } const previewText = localization.YOUR_VERIFICATION_CODE_IS_CODE.replace( "{verificationCode}", verificationCode ) return ( {head} {previewText}
{logoURL && (typeof logoURL === "string" ? ( {appName ) : ( <> {appName {appName ))} {localization.VERIFY_YOUR_EMAIL} {(() => { const textWithAppName = localization.WE_NEED_TO_VERIFY_YOUR_EMAIL_ADDRESS.replace( "{appName}", appName || "" ) .replace(/\s{2,}/g, " ") .replace(" .", ".") const [beforeEmail, afterEmail] = textWithAppName.split("{email}") return email ? ( <> {beforeEmail} {email} {afterEmail} ) : ( textWithAppName .replace("{email}", "") .replace(/\s{2,}/g, " ") .replace(" .", ".") ) })()}
{verificationCode}

{localization.THIS_CODE_EXPIRES_IN_MINUTES.replace( "{expirationMinutes}", expirationMinutes.toString() )} {appName && ( <> {" "} {localization.EMAIL_SENT_BY.replace("{appName}", appName)} )} {localization.IF_YOU_DIDNT_REQUEST_THIS_EMAIL} {poweredBy && ( {(() => { const [beforeBetterAuth, afterBetterAuth] = localization.POWERED_BY_BETTER_AUTH.split("{betterAuth}") return ( <> {beforeBetterAuth} better-auth {afterBetterAuth} ) })()} )}
) } OtpEmail.localization = otpEmailLocalization OtpEmail.PreviewProps = { verificationCode: "069420", email: "m@example.com", appName: "Better Auth", darkMode: true } as OtpEmailProps export default OtpEmail