🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Refactor OTP email handling in authentication module
Bill Yang9f73aa114f8a
diff --git a/server/src/lib/auth.ts b/server/src/lib/auth.tsindex 45f788114..9ee3dbc9f 100644--- a/server/src/lib/auth.ts+++ b/server/src/lib/auth.ts@@ -9,7 +9,7 @@ import { db } from "../db/postgres/postgres.js";import * as schema from "../db/postgres/schema.js";import { invitation, member, memberSiteAccess, user } from "../db/postgres/schema.js";import { DISABLE_SIGNUP, IS_CLOUD } from "./const.js";-import { sendEmail, sendInvitationEmail, sendWelcomeEmail } from "./email/email.js";+import { sendInvitationEmail, sendOtpEmail, sendWelcomeEmail } from "./email/email.js";dotenv.config();@@ -55,52 +55,7 @@ const pluginList = [}),emailOTP({async sendVerificationOTP({ email, otp, type }) {- let subject, htmlContent;-- if (type === "sign-in") {- subject = "Your Rybbit Sign-In Code";- htmlContent = `- <div style="font-family: sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; background-color: #0c0c0c; color: #e5e5e5;">- <h2 style="color: #ffffff; font-size: 24px; margin-bottom: 20px;">Your Sign-In Code</h2>- <p>Here is your one-time password to sign in to Rybbit:</p>- <div style="background-color: #1a1a1a; padding: 20px; border-radius: 6px; text-align: center; margin: 20px 0; font-size: 28px; letter-spacing: 4px; font-weight: bold; color: #10b981;">- ${otp}- </div>- <p>This code will expire in 5 minutes.</p>- <p>If you didn't request this code, you can safely ignore this email.</p>- </div>- `;- } else if (type === "email-verification") {- subject = "Verify Your Email Address";- htmlContent = `- <div style="font-family: sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; background-color: #0c0c0c; color: #e5e5e5;">- <h2 style="color: #ffffff; font-size: 24px; margin-bottom: 20px;">Verify Your Email</h2>- <p>Here is your verification code for Rybbit:</p>- <div style="background-color: #1a1a1a; padding: 20px; border-radius: 6px; text-align: center; margin: 20px 0; font-size: 28px; letter-spacing: 4px; font-weight: bold; color: #10b981;">- ${otp}- </div>- <p>This code will expire in 5 minutes.</p>- <p>If you didn't request this code, you can safely ignore this email.</p>- </div>- `;- } else if (type === "forget-password") {- subject = "Reset Your Password";- htmlContent = `- <div style="font-family: sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; background-color: #0c0c0c; color: #e5e5e5;">- <h2 style="color: #ffffff; font-size: 24px; margin-bottom: 20px;">Reset Your Password</h2>- <p>You requested to reset your password for Rybbit. Here is your one-time password:</p>- <div style="background-color: #1a1a1a; padding: 20px; border-radius: 6px; text-align: center; margin: 20px 0; font-size: 28px; letter-spacing: 4px; font-weight: bold; color: #10b981;">- ${otp}- </div>- <p>This code will expire in 5 minutes.</p>- <p>If you didn't request this code, you can safely ignore this email.</p>- </div>- `;- }-- if (subject && htmlContent) {- await sendEmail(email, subject, htmlContent);- }+ await sendOtpEmail(email, otp, type);},}),// Add Cloudflare Turnstile captcha (cloud only)diff --git a/server/src/lib/email/email.ts b/server/src/lib/email/email.tsindex 2f84bb682..75562c233 100644--- a/server/src/lib/email/email.ts+++ b/server/src/lib/email/email.ts@@ -3,6 +3,7 @@ import { render } from "@react-email/components";import { IS_CLOUD } from "../const.js";import { InvitationEmail } from "./templates/InvitationEmail.js";import { LimitExceededEmail } from "./templates/LimitExceededEmail.js";+import { OtpEmail, type OtpEmailType } from "./templates/OtpEmail.js";import { WeeklyReportEmail } from "./templates/WeeklyReportEmail.js";import type { OrganizationReport } from "../../services/weekyReports/weeklyReportTypes.js";@@ -32,6 +33,17 @@ export const sendEmail = async (email: string, subject: string, html: string) =>}};+const OTP_SUBJECTS: Record<OtpEmailType, string> = {+ "sign-in": "Your Rybbit Sign-In Code",+ "email-verification": "Verify Your Email Address",+ "forget-password": "Reset Your Password",+};++export const sendOtpEmail = async (email: string, otp: string, type: OtpEmailType) => {+ const html = await render(OtpEmail({ otp, type }));+ await sendEmail(email, OTP_SUBJECTS[type], html);+};+export const sendInvitationEmail = async (email: string,invitedBy: string,diff --git a/server/src/lib/email/templates/OtpEmail.tsx b/server/src/lib/email/templates/OtpEmail.tsxnew file mode 100644index 000000000..a0bd719be--- /dev/null+++ b/server/src/lib/email/templates/OtpEmail.tsx@@ -0,0 +1,101 @@+import {+ Body,+ Container,+ Head,+ Heading,+ Html,+ Preview,+ Section,+ Text,+ Tailwind,+ pixelBasedPreset,+} from "@react-email/components";+import * as React from "react";++export type OtpEmailType = "sign-in" | "email-verification" | "forget-password";++interface OtpEmailProps {+ otp: string;+ type: OtpEmailType;+}++const getContent = (type: OtpEmailType) => {+ switch (type) {+ case "sign-in":+ return {+ preview: "Your Rybbit Sign-In Code",+ heading: "Your Sign-In Code",+ description: "Here is your one-time password to sign in to Rybbit:",+ };+ case "email-verification":+ return {+ preview: "Verify Your Email Address",+ heading: "Verify Your Email",+ description: "Here is your verification code for Rybbit:",+ };+ case "forget-password":+ return {+ preview: "Reset Your Password",+ heading: "Reset Your Password",+ description: "You requested to reset your password for Rybbit. Here is your one-time password:",+ };+ }+};++export const OtpEmail = ({ otp, type }: OtpEmailProps) => {+ const currentYear = new Date().getFullYear();+ const content = getContent(type);++ return (+ <Html>+ <Head />+ <Preview>{content.preview}</Preview>+ <Tailwind+ config={{+ presets: [pixelBasedPreset],+ theme: {+ extend: {+ colors: {+ brand: "#10b981",+ lightBg: "#ffffff",+ cardBg: "#f9fafb",+ darkText: "#111827",+ mutedText: "#6b7280",+ borderColor: "#e5e7eb",+ },+ },+ },+ }}+ >+ <Body className="bg-lightBg font-sans">+ <Container className="mx-auto py-10 px-6 max-w-[600px]">+ <Section className="text-center">+ <Heading className="text-darkText text-3xl font-semibold mb-6">{content.heading}</Heading>+ </Section>++ <Section className="mb-6">+ <Text className="text-darkText text-base leading-relaxed mb-4">{content.description}</Text>+ </Section>++ <Section className="text-center mb-6">+ <div className="bg-cardBg py-5 px-6 rounded-md inline-block">+ <Text className="text-brand text-3xl font-bold tracking-widest m-0">{otp}</Text>+ </div>+ </Section>++ <Section className="mb-8">+ <Text className="text-mutedText text-sm leading-relaxed">This code will expire in 5 minutes.</Text>+ <Text className="text-mutedText text-sm leading-relaxed">+ If you didn't request this code, you can safely ignore this email.+ </Text>+ </Section>++ <Section className="text-center border-t border-borderColor pt-5">+ <Text className="text-mutedText text-xs">© {currentYear} Rybbit Analytics</Text>+ </Section>+ </Container>+ </Body>+ </Tailwind>+ </Html>+ );+};