TutorialBetter AuthAuthenticationNext.js

Better Auth email verification in Next.js, plus OTP, reset and email change

Wire every Better Auth email flow in a Next.js app to one send function: email verification, password reset, email change, email OTP and magic links.

Olivier Bazoud
November 14, 2026
November 15, 2026
12 min read

Better Auth never sends an email itself. Email verification, password reset, email change, email OTP and magic links each call a function you provide, and that function decides how the message leaves your app.

This guide sets up Better Auth email verification in a Next.js App Router project, then the other four flows.

FlowBetter Auth optionRecipient
Email verificationemailVerification.sendVerificationEmailThe address to verify
Password resetemailAndPassword.sendResetPasswordThe account address
Email changeuser.changeEmail.sendChangeEmailConfirmationThe current address
Email OTPemailOTP({ sendVerificationOTP })The address that asked for a code
Magic linkmagicLink({ sendMagicLink })The address that signs in

The code is typechecked against better-auth 1.7.25 and 1.6.20 with Next.js 16, and every flow calls one send function built on the Nuntly TypeScript SDK 2.0.0.

Before you start

You need a Next.js app with a database and Better Auth mounted on a route handler, as in Better Auth's Next.js integration:

TypeScript
// app/api/auth/[...all]/route.ts
import { toNextJsHandler } from 'better-auth/next-js';
import { auth } from '@/lib/auth';
export const { GET, POST } = toNextJsHandler(auth);

On the email side, you need three things:

  1. The SDK: npm install @nuntly/sdk.
  2. An API key with send permission, in NUNTLY_API_KEY. Sign up for the free plan if you have no account yet. It needs no credit card.
  3. A verified sending domain for the from address. Until the domain is verified, every send fails with domain_not_verified.

One send function for every Better Auth email

Every callback below calls the same sendEmail function. Changing the sender, the provider or the templates then touches one file.

TypeScript
// lib/email.ts
import { Nuntly } from '@nuntly/sdk';
const nuntly = new Nuntly({ apiKey: process.env.NUNTLY_API_KEY });
type AuthEmail = { to: string; subject: string; text: string; flow: string };
export async function sendEmail({ to, subject, text, flow }: AuthEmail) {
await nuntly.emails.send({
from: 'Acme <auth@mail.acme.com>',
to,
subject,
text,
tags: [{ name: 'flow', value: flow }],
});
}

The flow tag shows on the email's detail page in the Nuntly dashboard, so a password reset and a sign-in code are easy to tell apart when a user says nothing arrived.

The SDK retries transient failures and reuses one idempotency key across the retries of a call, so a retried send does not deliver the email twice.

The samples send plain text to stay short. Add an html field once you have templates, written with React Email or MJML.

Run the send in the background

A Better Auth callback should not hold up the HTTP response while the email sends. The email documentation says so for every send callback:

Avoid awaiting the email sending to prevent timing attacks.

If Better Auth waits for your callback before it responds, a password reset request for a registered address takes longer than one for an unknown address. The response time tells an attacker which accounts exist, the property the OWASP Forgot Password Cheat Sheet asks you to prevent.

The advanced.backgroundTasks.handler option fixes this in one place. When it is set, Better Auth hands the callback's promise to the handler and responds without waiting, so your callbacks can await the send. In Next.js, the handler is after:

TypeScript
// lib/auth.ts
import { betterAuth } from 'better-auth';
import { after } from 'next/server';
export const auth = betterAuth({
advanced: {
backgroundTasks: { handler: after },
},
// the flows below
});

after runs the send once the response is out, on a Node.js server, in Docker and on Vercel, where it keeps the function alive until the send finishes. The handler covers the verification emails sent on sign-up and sign-in, password reset, email change and email OTP. Two sends stay awaited whatever the handler: the resend endpoint behind authClient.sendVerificationEmail, which instead pads anonymous requests to at least 500 ms, and sendMagicLink.

Email verification in Next.js

Email verification sends a link to the address a user signed up with, and the user is verified when they open it. The options come from Better Auth's email and password docs. Each snippet from here on is one part of the betterAuth config in lib/auth.ts, so merge them into one call.

TypeScript
export const auth = betterAuth({
emailAndPassword: {
enabled: true,
requireEmailVerification: true,
},
emailVerification: {
sendOnSignUp: true,
autoSignInAfterVerification: true,
sendVerificationEmail: async ({ user, url }) => {
await sendEmail({
to: user.email,
subject: 'Verify your email address',
text: `Confirm your address: ${url}\n\nThe link expires in 1 hour.`,
flow: 'email-verification',
});
},
},
});
  • sendOnSignUp sends the link right after sign-up.
  • requireEmailVerification blocks sign-in until the address is verified. Add sendOnSignIn: true to send a fresh link on each blocked attempt.
  • autoSignInAfterVerification signs the user in when they open the link.
  • The link is valid for 1 hour by default. emailVerification.expiresIn changes it, in seconds.

The client calls in this guide use the authClient from lib/auth-client.ts. The OTP and magic link sections add their plugins to it.

TypeScript
// lib/auth-client.ts
import { createAuthClient } from 'better-auth/react';
export const authClient = createAuthClient();

A blocked sign-in returns a 403. Offer to send the link again, unless sendOnSignIn already did:

TypeScript
const { error } = await authClient.signIn.email({ email, password });
if (error?.status === 403) {
await authClient.sendVerificationEmail({ email, callbackURL: '/dashboard' });
}

Password reset

Password reset sends a one-time link to the account address. Better Auth returns the same response whether or not the address has an account.

TypeScript
export const auth = betterAuth({
emailAndPassword: {
enabled: true,
revokeSessionsOnPasswordReset: true,
sendResetPassword: async ({ user, url }) => {
await sendEmail({
to: user.email,
subject: 'Reset your password',
text: `Reset your password: ${url}\n\nThe link expires in 1 hour.`,
flow: 'reset-password',
});
},
},
});

The link expires after 1 hour by default (resetPasswordTokenExpiresIn, in seconds). revokeSessionsOnPasswordReset signs the account out everywhere once the new password is set, and it is off by default.

The client makes two calls. The first sends the email:

TypeScript
await authClient.requestPasswordReset({ email, redirectTo: '/reset-password' });

The link in the email goes through Better Auth, then lands on /reset-password?token=..., or ?error=INVALID_TOKEN when the link is expired or already used. The page reads the token from searchParams:

TypeScript
// app/reset-password/page.tsx
import { ResetPasswordForm } from './reset-password-form';
type Props = { searchParams: Promise<{ token?: string }> };
export default async function ResetPasswordPage({ searchParams }: Props) {
const { token } = await searchParams;
if (!token) return <p>This reset link is invalid or has expired.</p>;
return <ResetPasswordForm token={token} />;
}

A client component sets the new password:

TypeScript
// app/reset-password/reset-password-form.tsx
'use client';
import { useRouter } from 'next/navigation';
import { authClient } from '@/lib/auth-client';
export function ResetPasswordForm({ token }: { token: string }) {
const router = useRouter();
async function resetPassword(formData: FormData) {
const newPassword = String(formData.get('password'));
const { error } = await authClient.resetPassword({ newPassword, token });
if (!error) router.push('/sign-in');
}
return (
<form action={resetPassword}>
<input name="password" type="password" autoComplete="new-password" required />
<button type="submit">Set new password</button>
</form>
);
}

For the security reasoning behind each step, see how to implement password reset in Node.js.

Email change

Email change is off by default. Turn it on and give Better Auth a function that asks the current address to approve the change. It also needs the sendVerificationEmail callback from the previous section, and rejects the request without it.

TypeScript
export const auth = betterAuth({
user: {
changeEmail: {
enabled: true,
sendChangeEmailConfirmation: async ({ user, newEmail, url }) => {
await sendEmail({
to: user.email,
subject: 'Approve your email change',
text: `Approve the change of your account email to ${newEmail}: ${url}`,
flow: 'change-email',
});
},
},
},
});

For a user whose address is verified, the change takes two emails:

  1. sendChangeEmailConfirmation sends an approval link to the current address.
  2. Once it's approved, Better Auth calls your sendVerificationEmail with user.email set to the new address.
  3. The account email changes when the new address is verified.

Without sendChangeEmailConfirmation, Better Auth skips step 1 and only verifies the new address, so anyone holding a session can move the account to another mailbox. Keep the approval step. Better Auth also skips step 1 when the current address is unverified, so a test account created without verification only gets the second email.

Older tutorials set user.changeEmail.sendChangeEmailVerification. Neither 1.6.20 nor 1.7.25 has that option, and TypeScript doesn't flag it inside betterAuth({...}), so the callback silently never runs.

The client starts the change from the account settings page:

TypeScript
await authClient.changeEmail({ newEmail, callbackURL: '/settings' });

To change the address with a code instead of links, use the email OTP flow.

Email OTP

The emailOTP plugin replaces links with a code the user types. One sendVerificationOTP function covers its four uses, told apart by type:

TypeScript
import { emailOTP } from 'better-auth/plugins';
const otpSubjects = {
'sign-in': 'Your sign-in code',
'email-verification': 'Your verification code',
'forget-password': 'Your password reset code',
'change-email': 'Your email change code',
} as const;
export const auth = betterAuth({
plugins: [
emailOTP({
sendVerificationOTP: async ({ email, otp, type }) => {
await sendEmail({
to: email,
subject: otpSubjects[type],
text: `Your code is ${otp}. It expires in 5 minutes.`,
flow: `otp-${type}`,
});
},
}),
],
});

With strict TypeScript, indexing otpSubjects with a type it lacks is a compile error, so a new type in a future release fails the build instead of sending an email with no subject.

OptionDefaultEffect
otpLength6Digits in the code
expiresIn300Seconds before the code expires
allowedAttempts3Wrong guesses before the code is invalidated
disableSignUpfalseWhen false, an OTP sign-in creates an account for an unknown address
overrideDefaultEmailVerificationfalseWhen true, email verification sends a code instead of a link

Add emailOTPClient() to the client plugins, then sign in with a code in two calls:

TypeScript
// lib/auth-client.ts
import { createAuthClient } from 'better-auth/react';
import { emailOTPClient } from 'better-auth/client/plugins';
export const authClient = createAuthClient({ plugins: [emailOTPClient()] });
TypeScript
await authClient.emailOtp.sendVerificationOtp({ email, type: 'sign-in' });
await authClient.signIn.emailOtp({ email, otp });

The other types follow the same pattern: authClient.emailOtp.verifyEmail({ email, otp }) verifies an address, and authClient.emailOtp.requestPasswordReset({ email }) followed by authClient.emailOtp.resetPassword({ email, otp, password }) resets a password.

Email OTP also has its own email change, off by default. Set changeEmail: { enabled: true, verifyCurrentEmail: true } on the plugin. Without verifyCurrentEmail, a session alone can move the account to another mailbox, the gap the approval step closes in the link flow. The change then takes three calls:

TypeScript
// 1. A code to the current address
await authClient.emailOtp.sendVerificationOtp({ email: currentEmail, type: 'email-verification' });
// 2. That code approves the request, and a change-email code goes to the new address
await authClient.emailOtp.requestEmailChange({ newEmail, otp: currentEmailOtp });
// 3. The code from the new address applies the change
await authClient.emailOtp.changeEmail({ newEmail, otp: newEmailOtp });

That flow is where the change-email type in sendVerificationOTP comes from.

The magicLink plugin signs a user in from a link, with no password. The link works once and expires after 5 minutes by default.

TypeScript
import { magicLink } from 'better-auth/plugins';
export const auth = betterAuth({
plugins: [
magicLink({
sendMagicLink: async ({ email, url }) => {
await sendEmail({
to: email,
subject: 'Your sign-in link',
text: `Sign in: ${url}\n\nThe link expires in 5 minutes.`,
flow: 'magic-link',
});
},
}),
],
});

Like email OTP, it creates an account for an unknown address unless you set disableSignUp: true. On the client, add magicLinkClient() to the same plugins array in lib/auth-client.ts and call:

TypeScript
await authClient.signIn.magicLink({ email, callbackURL: '/dashboard' });

Better Auth awaits sendMagicLink directly, without the background handler, so a slow send delays this response.

One config for every flow with Better Email

Better Email is an open-source Better Auth plugin that replaces the callbacks above with one provider and one template renderer. In version 1.0.1 it registers the email verification and password reset callbacks itself. For email change and magic link, pass email.helpers.changeEmail to sendChangeEmailConfirmation and email.helpers.magicLink to sendMagicLink.

Email OTP is the exception. Keep the callback from this guide: the plugin's OTP helper doesn't accept the change-email type, and passing it to sendVerificationOTP fails to typecheck on Better Auth 1.6.20 and 1.7.25.

Pick the plugin if you want one place for templates and send hooks. Keep plain callbacks if you'd rather see each send in auth.ts.

Before you launch

Check these five points before real users sign up:

  1. The from address is on a verified sending domain, or every send fails with domain_not_verified.
  2. advanced.backgroundTasks.handler is set to after, so response times don't reveal which accounts exist.
  3. requireEmailVerification is on if unverified users must not sign in.
  4. Email OTP and magic link set disableSignUp: true, unless they should create accounts.
  5. A test sign-up receives the verification email, and its link verifies the address.

Every flow goes through one sendEmail function with a flow tag, so you change the sender or the templates in one file. The tag shows on each email's page in the Nuntly dashboard and in its webhook events.

The Better Auth page in the docs lists every callback with its option name.

On Supabase Auth instead of Better Auth? Send Supabase emails through Nuntly covers the same flows with custom SMTP or the Send Email Hook.

Ready to get started?

Ship emails, not infrastructure

Free plan available. No credit card required.

Start for free