diff --git a/apps/api/src/auth/auth.server.ts b/apps/api/src/auth/auth.server.ts index b54ba8a074..3301dda54b 100644 --- a/apps/api/src/auth/auth.server.ts +++ b/apps/api/src/auth/auth.server.ts @@ -1,5 +1,6 @@ import '../config/load-env'; import { + ChangeEmailConfirmationEmail, MagicLinkEmail, OTPVerificationEmail, VerifyEmail, @@ -572,6 +573,23 @@ export const auth = betterAuth({ socialProviders, user: { modelName: 'User', + changeEmail: { + enabled: true, + // Double opt-in: this link goes to the CURRENT address; confirming it + // makes better-auth send a verification link to the NEW address (via + // emailVerification.sendVerificationEmail), which applies the change. + sendChangeEmailConfirmation: async ({ user, newEmail, url }) => { + await triggerEmail({ + to: user.email, + subject: 'Confirm your email change for Comp AI', + react: ChangeEmailConfirmationEmail({ + currentEmail: user.email, + newEmail, + url, + }), + }); + }, + }, }, organization: { modelName: 'Organization', diff --git a/apps/api/src/email/templates/login-email-changed.tsx b/apps/api/src/email/templates/login-email-changed.tsx new file mode 100644 index 0000000000..ad59f5eece --- /dev/null +++ b/apps/api/src/email/templates/login-email-changed.tsx @@ -0,0 +1,80 @@ +import { + Body, + Container, + Font, + Heading, + Html, + Preview, + Section, + Tailwind, + Text, +} from '@react-email/components'; +import { Footer } from '../components/footer'; +import { Logo } from '../components/logo'; + +interface Props { + organizationName: string; + oldEmail: string; + newEmail: string; +} + +export const LoginEmailChangedEmail = ({ organizationName, oldEmail, newEmail }: Props) => { + return ( + + + + + + + Your Comp AI login email was changed + + + + + + Your login email was changed + + + + An administrator of {organizationName}{' '} + changed your Comp AI login email from{' '} + {oldEmail} to{' '} + {newEmail}. + + + From now on, use {newEmail} to + sign in. + + +
+
+ + If you did not expect this change, contact your organization + administrator or support@trycomp.ai. + +
+ +
+ +