How to set CallbackURL in emailVerification
I'm using BetterAuth for email verification in my app, and I need help setting the callbackURL for the emailVerification process. In my auth.ts file, I'm trying to send a verification email, but I can't figure out how to properly set the callbackURL that the user will be redirected to after clicking the confirmation link.
How do I configure the callbackURL in the emailVerification setup in BetterAuth?
5 Replies
You can specify the callback URL only when triggering sendVerificationEmail manually:
https://www.better-auth.com/docs/concepts/email#3-manually
Email | Better Auth
Learn how to use email with Better Auth.
Otherwise callback in signUp/In.email is used to redirect user after successful email verification
@Glen Kurio thanks for pointing us in the right direction...if the only way to show a custom email verification call back page is to send the verification email manually, does this mean that the configuration from our auth file (where we instantiate
betterAuth()
with config object ) shoudl be removed?
i mean this config block:
emailVerification: {
sendOnSignUp: true,
autoSignInAfterVerification: false,
sendVerificationEmail: async ({ user, url }) => {
await resend.emails.send({
from: EMAIL_FROM_FULL,
to: [user.email],
subject: 'Confirm Your Email on PLANTKR.com',
react: ConfirmEmail({ userEmail: user.email, confirmUrl: url }),
});
}
},
thanks!
I added the code to manually send the confirmation email in my 'signup' form submit function (i'm using Next.js 15) and now I am getting the email sent twice at the same time. The configuration/documentation isnt clear as to how to do this. I i remove the entire emailVerification: {...} block then I get 'email verification is not enabled' error...any tips? thanks.Not sure what you are trying to do? From what you described verification email sent twice because you trigger it twice in your code.
@Glen Kurio I am using Next.js + BetterAuth for user signup/login/session management. I have my betterauth config that looks like this:
const auth = betterAuth({
database: mongodbAdapter(db),
emailAndPassword: {
enabled: true,
resetPasswordTokenExpiresIn: 300,
requireEmailVerification: true,
sendResetPassword: async ({ user, url }) => {
await resend.emails.send({
from: EMAIL_FROM_FULL,
to: [user.email],
subject: 'Reset Your Password on PLANTKR.com',
react: ForgotPasswordEmail({ userEmail: user.email, resetUrl: url }),
});
},
},
emailVerification: {
autoSignInAfterVerification: true,
sendVerificationEmail: async ({ user, url }) => {
await resend.emails.send({
from: EMAIL_FROM_FULL,
to: [user.email],
subject: 'Confirm Your Email on PLANTKR.com',
react: ConfirmEmail({ userEmail: user.email, confirmUrl: url }),
});
}
},
socialProviders: {
google: {
clientId: process.env.AUTH_GOOGLE_ID as string,
clientSecret: process.env.AUTH_GOOGLE_SECRET as string,
},
},
plugins: [nextCookies()], // nextCookies MUST be the last item
});
the above works and sends the email to the user automatically, but when this email is sent, i want to 'catch' the user clicking on the email link to confirm email so that i can show them a verify email page AND to refresh the page to update UI elements to show user is no automatically logged in.
so i found your response above and implemented the manual email verification trigger for betterauth, and it works but as a result, i now get the email sent twice and not just once automatically like before. I dont know how to disable the automatic email sending and just have it so that the manual sending is the only one that works.
do you know where I can find the precise documentation for betterAuth()
function and all the available options for emailVerification
prop?
thank you