Site icon JD Bots

How to Build a Sign-In Form with Better Auth in Next.js

A dark indigo card titled Better Auth Sign-In Form, labelled Better Auth How-To, with the line Sign users in to a Next.js app with authClient.signIn.email, beside a drawing of a Sign in form with email and password fields and a Sign in button.

How to build a sign-in form with Better Auth in Next.js.

A sign-in form checks an email address and a password and starts a session. This guide builds a Better Auth sign-in form in Next.js with authClient.signIn.email, and shows what a wrong password looks like in the browser and in the Terminal.

The short answer: in a client component, call authClient.signIn.email({ email, password }). When the password is right, Better Auth starts a session and sets its cookie; when it’s wrong, error.message says Invalid email or password.

Syntax

Syntax

const { data, error } = await authClient.signIn.email({
  email: "<email address>",
  password: "<password>",
  rememberMe: <true or false>,
  callbackURL: "<page address>",
});

The reference: Sign In in the Better Auth documentation.

Prerequisites

Step 1: Create the Sign-In Page

The example is a small bookshop app with a header that shows who’s signed in. In the app folder, create a folder named sign-in, and in it a file named page.tsx:

app/sign-in/page.tsx

"use client";

import Link from "next/link";
import { useRouter } from "next/navigation";
import { type FormEvent, useState } from "react";
import { authClient } from "@/lib/auth-client";

export default function SignInPage() {
  const router = useRouter();
  const [errorMessage, setErrorMessage] = useState("");
  const [isPending, setIsPending] = useState(false);

  async function handleSubmit(event: FormEvent<HTMLFormElement>) {
    event.preventDefault();
    const form = new FormData(event.currentTarget);
    setErrorMessage("");
    setIsPending(true);

    const { error } = await authClient.signIn.email({
      email: String(form.get("email")),
      password: String(form.get("password")),
    });

    setIsPending(false);
    if (error) {
      setErrorMessage(error.message ?? "Sign-in failed. Please try again.");
      return;
    }
    router.push("/");
  }

  return (
    <main className="mx-auto w-full max-w-sm flex-1 px-6 py-12">
      <h1 className="text-2xl font-semibold">Sign in</h1>
      <form onSubmit={handleSubmit} className="mt-6 flex flex-col gap-4">
        <label className="flex flex-col gap-1 text-sm">
          Email
          <input
            name="email"
            type="email"
            required
            autoComplete="email"
            className="rounded border border-zinc-300 px-3 py-2 dark:border-zinc-700"
          />
        </label>
        <label className="flex flex-col gap-1 text-sm">
          Password
          <input
            name="password"
            type="password"
            required
            autoComplete="current-password"
            className="rounded border border-zinc-300 px-3 py-2 dark:border-zinc-700"
          />
        </label>
        {errorMessage && (
          <p role="alert" className="text-sm text-red-600">
            {errorMessage}
          </p>
        )}
        <button
          type="submit"
          disabled={isPending}
          className="rounded bg-zinc-900 px-4 py-2 text-white disabled:opacity-50 dark:bg-zinc-100 dark:text-zinc-900"
        >
          {isPending ? "Signing in…" : "Sign in"}
        </button>
      </form>
      <p className="mt-6 text-sm">
        New to the Bookshop?{" "}
        <Link href="/sign-up" className="underline">
          Create an account
        </Link>
      </p>
    </main>
  );
}

Open /sign-in in your browser:

The sign-in page.

Step 2: Sign In

Enter an email address with a wrong password and click Sign in:

A wrong password gets a message, not a session.

The Terminal running the app logs the failed attempt:

Output

2026-11-19T04:21:16.447Z WARN [Better Auth]: Invalid password
POST /api/auth/sign-in/email 401 in 111ms (next.js: 3ms, application-code: 108ms)

Now enter the right password. The home page opens with the person signed in:

Signed in: the header shows the name.

Better Auth sets a cookie named better-auth.session_token. It’s HttpOnly, so scripts in the page can’t read it, and it lasts 7 days by default.

Troubleshooting

MessageCauseFix
Invalid email or passwordThe password, or the email address, is wrongCheck both and try again.

FAQ

How do I keep people signed in only until they close the browser?

Pass rememberMe: false to signIn.email.

Can people sign in with a username?

Yes, with Better Auth’s username plugin.

Conclusion

authClient.signIn.email checks the password and starts a session. Show error.message when it fails, so the person knows to try again.

Additional Resources

Related

Better Auth, Next.js and other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by Better Auth, Inc. or Vercel.

Exit mobile version