Issue with Better-Auth Email Verification

Hey everyone, I'm having an issue with email verification in better-auth. When a user changes their email: The email address gets updated correctly in the database BUT the verification email is not being sent Here's the relevant code:
// auth.js config
user: {
changeEmail: {
enabled: true,
sendChangeEmailVerification: async ({ user, newEmail, url, token }) => {
console.log("sendChangeEmailVerification", user, newEmail, url, token);

await sendEmail({
to: newEmail,
subject: "Vérification d'email",
text: `Cliquez sur ce lien pour vérifier votre nouvelle adresse email: ${url}`,
});
},
},
// ...
}

// action.js (server action)
export async function updateUserEmail(email: string) {
await auth.api.changeEmail({
body: {
newEmail: email,
callbackURL: `/`,
},
headers: await headers(),
})
}
// auth.js config
user: {
changeEmail: {
enabled: true,
sendChangeEmailVerification: async ({ user, newEmail, url, token }) => {
console.log("sendChangeEmailVerification", user, newEmail, url, token);

await sendEmail({
to: newEmail,
subject: "Vérification d'email",
text: `Cliquez sur ce lien pour vérifier votre nouvelle adresse email: ${url}`,
});
},
},
// ...
}

// action.js (server action)
export async function updateUserEmail(email: string) {
await auth.api.changeEmail({
body: {
newEmail: email,
callbackURL: `/`,
},
headers: await headers(),
})
}
The console.log shows nothing when changing email, and no verification email is sent. The email does update in the database though. Any ideas what might be wrong with my implementation?
6 Replies
rhitune
rhitune2w ago
where is send mail codes?
rhitune
rhitune2w ago
also if db updating and mail not sending , its about your mail sending function not better auth
No description
Jïns
JïnsOP2w ago
Yeah but I did this
console.log("sendChangeEmailVerification", user, newEmail, url, token);
console.log("sendChangeEmailVerification", user, newEmail, url, token);
to see if the function get triggered but nothing
rhitune
rhitune2w ago
send your full auth config please
Jïns
JïnsOP5d ago
import { sendEmail } from "@/lib/SendEmail";
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { adminClient } from "better-auth/client/plugins";
import { nextCookies } from "better-auth/next-js";
import { admin } from "better-auth/plugins";
import { db } from "../../db/connection";

export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
}),

emailAndPassword: {
enabled: true,
sendResetPassword: async ({ user, url }) => {
await sendEmail({
from: process.env.EMAIL_NODEMAILER || "",
to: user.email,
subject: "Password Reset",
text: `Click the link to reset your password: ${url}`,
});
},
},
user: {
changeEmail: {
enabled: true,
sendChangeEmailVerification: async ({ newEmail, url }) => {
// Il faudrait ajouter ceci:
await sendEmail({
from: process.env.EMAIL_NODEMAILER || "",
to: newEmail, // Envoyer à la nouvelle adresse email
subject: "Vérification d'email",
text: `Cliquez sur ce lien pour vérifier votre nouvelle adresse email: ${url}`,
});
},
},
additionalFields: {
truc_de_con: {
type: "string",
nullable: true,
},
},
},
plugins: [nextCookies(), admin(), adminClient()],
});

export type Session = typeof auth.$Infer.Session;
import { sendEmail } from "@/lib/SendEmail";
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { adminClient } from "better-auth/client/plugins";
import { nextCookies } from "better-auth/next-js";
import { admin } from "better-auth/plugins";
import { db } from "../../db/connection";

export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
}),

emailAndPassword: {
enabled: true,
sendResetPassword: async ({ user, url }) => {
await sendEmail({
from: process.env.EMAIL_NODEMAILER || "",
to: user.email,
subject: "Password Reset",
text: `Click the link to reset your password: ${url}`,
});
},
},
user: {
changeEmail: {
enabled: true,
sendChangeEmailVerification: async ({ newEmail, url }) => {
// Il faudrait ajouter ceci:
await sendEmail({
from: process.env.EMAIL_NODEMAILER || "",
to: newEmail, // Envoyer à la nouvelle adresse email
subject: "Vérification d'email",
text: `Cliquez sur ce lien pour vérifier votre nouvelle adresse email: ${url}`,
});
},
},
additionalFields: {
truc_de_con: {
type: "string",
nullable: true,
},
},
},
plugins: [nextCookies(), admin(), adminClient()],
});

export type Session = typeof auth.$Infer.Session;
sorry for the response time
rhitune
rhitune5d ago
@bekacru any idea?

Did you find this page helpful?