Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Implement email change verification feature

Bill Yang601b6e2da6b0
diff --git a/server/src/lib/auth.ts b/server/src/lib/auth.ts
index e57e7ae75..e5b2a9aa2 100644
--- a/server/src/lib/auth.ts
+++ b/server/src/lib/auth.ts
@@ -11,7 +11,13 @@ 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 { API_RATE_LIMIT_WINDOW, DISABLE_SIGNUP, IS_CLOUD, STANDARD_API_RATE_LIMIT } from "./const.js";
-import { addContactToAudience, sendInvitationEmail, sendOtpEmail, sendWelcomeEmail } from "./email/email.js";
+import {
+ addContactToAudience,
+ sendChangeEmailVerification,
+ sendInvitationEmail,
+ sendOtpEmail,
+ sendWelcomeEmail,
+} from "./email/email.js";
import { onboardingTipsService } from "../services/onboardingTips/onboardingTipsService.js";
dotenv.config();
@@ -135,6 +141,18 @@ export const auth = betterAuth({
},
changeEmail: {
enabled: true,
+ sendChangeEmailConfirmation: async ({
+ user,
+ newEmail,
+ url,
+ }: {
+ user: { email: string };
+ newEmail: string;
+ url: string;
+ token: string;
+ }) => {
+ await sendChangeEmailVerification(user.email, newEmail, url);
+ },
},
},
plugins: pluginList,
diff --git a/server/src/lib/email/email.ts b/server/src/lib/email/email.ts
index 411ac9655..614e8ce17 100644
--- a/server/src/lib/email/email.ts
+++ b/server/src/lib/email/email.ts
@@ -104,6 +104,26 @@ export const sendOtpEmail = async (email: string, otp: string, type: OtpEmailTyp
await sendEmail(email, OTP_SUBJECTS[type], html);
};
+export const sendChangeEmailVerification = async (
+ currentEmail: string,
+ newEmail: string,
+ verificationUrl: string
+) => {
+ const html = `
+ <div style="font-family: Arial, sans-serif; max-width: 560px; margin: 0 auto; padding: 24px; color: #111;">
+ <h2 style="margin: 0 0 16px;">Confirm your new email</h2>
+ <p>We received a request to change the email on your Rybbit account from <strong>${currentEmail}</strong> to <strong>${newEmail}</strong>.</p>
+ <p>Click the button below to confirm the change. If you didn't request this, you can safely ignore this email.</p>
+ <p style="margin: 24px 0;">
+ <a href="${verificationUrl}" style="background: #111; color: #fff; padding: 12px 20px; border-radius: 6px; text-decoration: none; display: inline-block;">Confirm email change</a>
+ </p>
+ <p style="font-size: 12px; color: #666; word-break: break-all;">Or paste this link into your browser: ${verificationUrl}</p>
+ </div>
+ `;
+
+ await sendEmail(currentEmail, "Confirm your email change on Rybbit", html);
+};
+
export const sendInvitationEmail = async (
email: string,
invitedBy: string,