DEV Community

Cover image for Email Verification with Better-Auth (Basics Tutorial, Ep. 2)
Zul Ikram Musaddik Rayat
Zul Ikram Musaddik Rayat

Posted on

Email Verification with Better-Auth (Basics Tutorial, Ep. 2)

Welcome back to the Better-Auth Basics series! ๐Ÿš€

In Episode 1, we set up Better-Auth with Drizzle ORM and implemented sign-up and login functionality. It worked greatโ€ฆ but thereโ€™s a big flaw:

๐Ÿ‘‰ Anyone can sign up with any random email โ€” even one they donโ€™t own.

Thatโ€™s obviously not safe for production. So in this post, weโ€™ll fix that by adding Email Verification with Better-Auth.


โŒ The Problem Without Verification

Right now, a user can type any email address on the registration page, and the server will happily accept it.

That means fake accounts, spam signups, and security risks. We need a way to make sure the person actually owns the email theyโ€™re registering with.


โœ… Email Verification with Better-Auth

The good news? Better-Auth already has email verification built in for email/password authentication. We just need to configure it. Letโ€™s go step by step.


๐Ÿ›  Step 1: Configure an Email Provider

For sending verification emails, I used Resend.

  1. Create a Resend account
  2. Add your custom domain
  3. Generate an API key
  4. Save it in your .env file:
RESEND_API_KEY=your-api-key
Enter fullscreen mode Exit fullscreen mode

๐Ÿ›  Step 2: Create Email Templates

Inside Resend, configure simple templates for your verification emails.
This is the message your users will see in their inbox with the verification link.


๐Ÿ›  Step 3: Enable Email Verification in Better-Auth

In your Better-Auth configuration, enable the verification features:

import { betterAuth } from "better-auth/server";
import { schema } from "../db/schema";

export const auth = betterAuth({
  schema,
  emailAndPassword: {
    enabled: true,
    requireEmailVerification: true,
  },
  emailVerification: {
    sendOnSignUp: true,
    autoSignInAfterVerification: true,
    async sendVerificationEmail({ user, url, token }, request) {
      await sendVerificationEmail({ email: user.email, url, token });
    },
  },
});
Enter fullscreen mode Exit fullscreen mode

๐Ÿ›  Step 4: Send Verification Email on Sign Up

Better-Auth will now automatically send a verification email when a new user registers.

  • After sign-up, redirect the user to a verify page in your app.
  • Tell them to check their inbox for the verification link.
// app/verify/page.tsx
export default function VerifyPage() {
  return (
    <div>
      <h1>Verify Your Email</h1>
      <p>
        Weโ€™ve sent you a link. Please check your inbox and click it to activate your account.
      </p>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ›  Step 5: Testing the Flow

  • Register with a real email address
  • Check your inbox โ†’ youโ€™ll see the verification email
  • Click the link โ†’ the server validates it
  • Youโ€™re automatically signed in and redirected to the homepage ๐ŸŽ‰

Hereโ€™s what the successful response looks like:

{
  "status": 200,
  "message": "Email verified and user signed in"
}
Enter fullscreen mode Exit fullscreen mode

๐ŸŽ‰ And Thatโ€™s It!

With just a few lines of configuration, we added secure email verification to our Better-Auth setup.

Now:

  • Users must prove they own their email
  • Fake signups are blocked
  • Onboarding is safer and more production-ready

๐Ÿ“Œ Whatโ€™s Next?

This was Episode 2 of Better-Auth Basics.
In future episodes, weโ€™ll cover:

  • ๐Ÿ‘ฅ Role-based authentication
  • โšก Rate limiting
  • ๐Ÿ›ก Middleware for protecting routes Stay tuned โ€” weโ€™re just getting started.

๐Ÿ”— Stay Connected

๐Ÿ’ฌ Got questions? Drop them in the comments โ€” I reply to every one!
๐Ÿ‘ Donโ€™t forget to like, share, and subscribe for more dev content.

Top comments (0)