Skip to main content
Prisma Documentation Docs
current

Search documentation

Type to search this documentation.

On this pageOverview

Better Auth (with Next.js)

Learn how to use Prisma ORM in a Next.js app with Better Auth

Better Auth is an open-source authentication library for web applications. It's written in TypeScript, can be extended with plugins, and supports multiple database adapters, including Prisma.

In this guide, you'll wire Better Auth into a brand-new Next.js app and persist users in a Prisma Postgres database. You can find a complete example of this guide on GitHub.

  • Node.js 20+
  • Basic familiarity with Next.js App Router and Prisma

Create a new Next.js application:

bun
bunx create-next-app@latest betterauth-nextjs-prisma
pnpm
pnpm dlx create-next-app@latest betterauth-nextjs-prisma
yarn
yarn dlx create-next-app@latest betterauth-nextjs-prisma
npm
npx create-next-app@latest betterauth-nextjs-prisma

It will prompt you to customize your setup. Choose the defaults:

Navigate to the project directory:

Bash
cd betterauth-nextjs-prisma

These selections create a Next.js project with TypeScript for type safety, ESLint for code quality, and Tailwind CSS for styling. Using the src/ directory and the App Router are common conventions for new Next.js applications.

Next, you'll add Prisma to your project to manage your database.

Install the necessary Prisma packages. The dependencies differ slightly depending on whether you use Prisma Postgres with Accelerate or another database.

bun
bun add prisma@prev tsx @types/pg --dev
pnpm
pnpm add prisma@prev tsx @types/pg --save-dev
yarn
yarn add prisma@prev tsx @types/pg --dev
npm
npm install prisma@prev tsx @types/pg --save-dev
bun
bun add @prisma/client@7 @prisma/adapter-pg dotenv pg
pnpm
pnpm add @prisma/client@7 @prisma/adapter-pg dotenv pg
yarn
yarn add @prisma/client@7 @prisma/adapter-pg dotenv pg
npm
npm install @prisma/client@7 @prisma/adapter-pg dotenv pg

Once installed, initialize Prisma in your project:

bun
bunx --bun prisma init --output ../src/generated/prisma
pnpm
pnpm prisma init --output ../src/generated/prisma
yarn
yarn prisma init --output ../src/generated/prisma
npm
npx prisma init --output ../src/generated/prisma

This will create:

  • A prisma directory with a schema.prisma file
  • A .env file containing a local DATABASE_URL at the project root
  • An output directory for the generated Prisma Client as better-auth/generated/prisma

Create a Prisma Postgres database and replace the generated DATABASE_URL in your .env file with the postgres://... connection string from the CLI output:

bun
bunx create-db
pnpm
pnpm dlx create-db
yarn
yarn dlx create-db
npm
npx create-db

Create a prisma.config.ts file in the root of your project with the following content:

prisma.config.ts
import "dotenv/config";
import { defineConfig, env } from "prisma/config";

export default defineConfig({
  schema: "prisma/schema.prisma",
  migrations: {
    path: "prisma/migrations",
  },
  datasource: {
    url: env("DATABASE_URL"),
  },
});
```bash
bun add dotenv
```
```bash
pnpm add dotenv
```
```bash
yarn add dotenv
```
```bash
npm install dotenv
```

Run the following command to create the database tables and generate the Prisma Client:

bun
bunx prisma generate
pnpm
pnpm prisma generate
yarn
yarn prisma generate
npm
npx prisma generate

In the src directory, create a lib folder and a prisma.ts file inside it. This file will be used to create and export your Prisma Client instance.

Bash
mkdir -p src/lib
touch src/lib/prisma.ts

Set up the Prisma client like this:

src/lib/prisma.ts
import { PrismaClient } from "@/generated/prisma/client";
import { PrismaPg } from "@prisma/adapter-pg";

const adapter = new PrismaPg({
  connectionString: process.env.DATABASE_URL!,
});

const globalForPrisma = global as unknown as {
  prisma: PrismaClient;
};

const prisma =
  globalForPrisma.prisma ||
  new PrismaClient({
    adapter,
  });

if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;

export default prisma;

Next, add Better Auth to handle sign-up, sign-in, and sessions.

First, install the Better Auth core package:

bun
bun add better-auth
pnpm
pnpm add better-auth
yarn
yarn add better-auth
npm
npm install better-auth

Next, generate a secure secret that Better Auth will use to sign authentication tokens. This ensures your tokens cannot be messed with.

bun
bunx auth@latest secret
pnpm
pnpm dlx auth@latest secret
yarn
yarn dlx auth@latest secret
npm
npx auth@latest secret

Copy the generated secret and add it, along with your application's URL, to your .env file:

.env
# Better AuthBETTER_AUTH_SECRET=your-generated-secretBETTER_AUTH_URL=http://localhost:3000# PrismaDATABASE_URL="your-database-url"

Now, create a configuration file for Better Auth. In the src/lib directory, create an auth.ts file:

Bash
touch src/lib/auth.ts

In this file, you'll configure Better Auth to use the Prisma adapter, which allows it to persist user and session data in your database. You will also enable email and password authentication.

src/lib/auth.ts
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import prisma from "@/lib/prisma";

export const auth = betterAuth({
  database: prismaAdapter(prisma, {
    provider: "postgresql",
  }),
});

Better Auth also supports other sign-in methods like social logins (Google, GitHub, etc.), which you can explore in their email and password documentation.

src/lib/auth.ts
import { betterAuth } from "better-auth";import { prismaAdapter } from "better-auth/adapters/prisma";import prisma from "@/lib/prisma";export const auth = betterAuth({  database: prismaAdapter(prisma, {    provider: "postgresql",  }),  emailAndPassword: {    enabled: true,  },});

Better Auth provides a CLI command to automatically add the necessary authentication models (User, Session, Account, and Verification) to your schema.prisma file.

Run the following command:

bun
bunx auth generate
pnpm
pnpm dlx auth generate
yarn
yarn dlx auth generate
npm
npx auth generate

This will add the following models:

prisma
model User {
  id            String    @id
  name          String
  email         String
  emailVerified Boolean
  image         String?
  createdAt     DateTime
  updatedAt     DateTime
  sessions      Session[]
  accounts      Account[]

  @@unique([email])
  @@map("user")
}

model Session {
  id        String   @id
  expiresAt DateTime
  token     String
  createdAt DateTime
  updatedAt DateTime
  ipAddress String?
  userAgent String?
  userId    String
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([token])
  @@map("session")
}

model Account {
  id                    String    @id
  accountId             String
  providerId            String
  userId                String
  user                  User      @relation(fields: [userId], references: [id], onDelete: Cascade)
  accessToken           String?
  refreshToken          String?
  idToken               String?
  accessTokenExpiresAt  DateTime?
  refreshTokenExpiresAt DateTime?
  scope                 String?
  password              String?
  createdAt             DateTime
  updatedAt             DateTime

  @@map("account")
}

model Verification {
  id         String    @id
  identifier String
  value      String
  expiresAt  DateTime
  createdAt  DateTime?
  updatedAt  DateTime?

  @@map("verification")
}

With the new models in your schema, you need to update your database. Run a migration to create the corresponding tables:

bun
bunx prisma migrate dev --name add-auth-models
pnpm
pnpm prisma migrate dev --name add-auth-models
yarn
yarn prisma migrate dev --name add-auth-models
npm
npx prisma migrate dev --name add-auth-models
bun
bunx prisma generate
pnpm
pnpm prisma generate
yarn
yarn prisma generate
npm
npx prisma generate

Better Auth needs an API endpoint to handle authentication requests like sign-in, sign-up, and sign-out. You'll create a catch-all API route in Next.js to handle all requests sent to /api/auth/[...all].

In the src/app/api directory, create an auth/[...all] folder structure and a route.ts file inside it:

Bash
mkdir -p "src/app/api/auth/[...all]"
touch "src/app/api/auth/[...all]/route.ts"

Add the following code to the newly created route.ts file. This code uses a helper from Better Auth to create Next.js-compatible GET and POST request handlers.

TypeScript
import { auth } from "@/lib/auth";
import { toNextJsHandler } from "better-auth/next-js";

export const { POST, GET } = toNextJsHandler(auth);

Next, you'll need a client-side utility to interact with these endpoints from your React components. In the src/lib directory, create an auth-client.ts file:

Bash
touch src/lib/auth-client.ts

Add the following code, which creates the React hooks and functions you'll use in your UI:

TypeScript
import { createAuthClient } from "better-auth/react";

export const { signIn, signUp, signOut, useSession } = createAuthClient();

Next, build the user interface for authentication. In the src/app directory, create the following folder structure:

  • sign-up/page.tsx
  • sign-in/page.tsx
  • dashboard/page.tsx
Bash
mkdir -p src/app/{sign-up,sign-in,dashboard}
touch src/app/{sign-up,sign-in,dashboard}/page.tsx

First, create the basic SignUpPage component in src/app/sign-up/page.tsx. This sets up the main container and a title for your page.

src/app/sign-up/page.tsx
"use client";

export default function SignUpPage() {
  return (
    <main className="max-w-md mx-auto p-6 space-y-4 text-white">
      <h1 className="text-2xl font-bold">Sign Up</h1>
    </main>
  );
}

Next, import the necessary hooks from React and Next.js to manage state and navigation. Initialize the router and a state variable to hold any potential error messages.

src/app/sign-up/page.tsx
"use client";import { useState } from "react";import { useRouter } from "next/navigation";export default function SignUpPage() {  const router = useRouter();  const [error, setError] = useState<string | null>(null);  return (    <main className="max-w-md mx-auto p-6 space-y-4 text-white">      <h1 className="text-2xl font-bold">Sign Up</h1>    </main>  );}

Now, import the signUp function from your Better Auth client and add the handleSubmit function. This function is triggered on form submission and calls the signUp.email method provided by Better Auth, passing the user's name, email, and password.

src/app/sign-up/page.tsx
"use client";import { useState } from "react";import { useRouter } from "next/navigation";//add-next-linimport { signUp } from "@/lib/auth-client";export default function SignUpPage() {  const router = useRouter();  const [error, setError] = useState<string | null>(null);  async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {    e.preventDefault();    setError(null);    const formData = new FormData(e.currentTarget);    const res = await signUp.email({      name: formData.get("name") as string,      email: formData.get("email") as string,      password: formData.get("password") as string,    });    if (res.error) {      setError(res.error.message || "Something went wrong.");    } else {      router.push("/dashboard");    }  }  return (    <main className="max-w-md mx-auto p-6 space-y-4 text-white">      <h1 className="text-2xl font-bold">Sign Up</h1>    </main>  );}

To inform the user of any issues, add an element that conditionally renders when the error state is not null.

src/app/sign-up/page.tsx
"use client";import { useState } from "react";import { useRouter } from "next/navigation";import { signUp } from "@/lib/auth-client";export default function SignUpPage() {  const router = useRouter();  const [error, setError] = useState<string | null>(null);  async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {    e.preventDefault();    setError(null);    const formData = new FormData(e.currentTarget);    const res = await signUp.email({      name: formData.get("name") as string,      email: formData.get("email") as string,      password: formData.get("password") as string,    });    if (res.error) {      setError(res.error.message || "Something went wrong.");    } else {      router.push("/dashboard");    }  }  return (    <main className="max-w-md mx-auto p-6 space-y-4 text-white">      <h1 className="text-2xl font-bold">Sign Up</h1>      {error && <p className="text-red-500">{error}</p>}    </main>  );}

Finally, add the HTML form with input fields for the user's name, email, and password, and a submit button.

src/app/sign-up/page.tsx
"use client";import { useState } from "react";import { useRouter } from "next/navigation";import { signUp } from "@/lib/auth-client";export default function SignUpPage() {  const router = useRouter();  const [error, setError] = useState<string | null>(null);  async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {    e.preventDefault();    setError(null);    const formData = new FormData(e.currentTarget);    const res = await signUp.email({      name: formData.get("name") as string,      email: formData.get("email") as string,      password: formData.get("password") as string,    });    if (res.error) {      setError(res.error.message || "Something went wrong.");    } else {      router.push("/dashboard");    }  }  return (    <main className="max-w-md mx-auto p-6 space-y-4 text-white">      <h1 className="text-2xl font-bold">Sign Up</h1>      {error && <p className="text-red-500">{error}</p>}      <form onSubmit={handleSubmit} className="space-y-4">        {" "}        <input          name="name"          placeholder="Full Name"          required          className="w-full rounded-md bg-neutral-900 border border-neutral-700 px-3 py-2"        />{" "}        <input          name="email"          type="email"          placeholder="Email"          required          className="w-full rounded-md bg-neutral-900 border border-neutral-700 px-3 py-2"        />{" "}        <input          name="password"          type="password"          placeholder="Password"          required          minLength={8}          className="w-full rounded-md bg-neutral-900 border border-neutral-700 px-3 py-2"        />{" "}        <button          type="submit"          className="w-full bg-white text-black font-medium rounded-md px-4 py-2 hover:bg-gray-200"        >          {" "}          // [!code ++] Create Account        </button>{" "}      </form>{" "}    </main>  );}

For the sign-in page, start with the basic structure in src/app/sign-in/page.tsx.

src/app/sign-in/page.tsx
"use client";

export default function SignInPage() {
  return (
    <main className="max-w-md h-screen flex items-center justify-center flex-col mx-auto p-6 space-y-4 text-white">
      <h1 className="text-2xl font-bold">Sign In</h1>
    </main>
  );
}

Now, add the state and router hooks, similar to the sign-up page.

src/app/sign-in/page.tsx
"use client";import { useState } from "react";import { useRouter } from "next/navigation";export default function SignInPage() {  const router = useRouter();  const [error, setError] = useState<string | null>(null);  return (    <main className="max-w-md h-screen flex items-center justify-center flex-col mx-auto p-6 space-y-4 text-white">      <h1 className="text-2xl font-bold">Sign In</h1>    </main>  );}

Add the handleSubmit function, this time importing and using the signIn.email method from Better Auth.

src/app/sign-in/page.tsx
"use client";import { useState } from "react";import { useRouter } from "next/navigation";import { signIn } from "@/lib/auth-client";export default function SignInPage() {  const router = useRouter();  const [error, setError] = useState<string | null>(null);  async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {    e.preventDefault();    setError(null);    const formData = new FormData(e.currentTarget);    const res = await signIn.email({      email: formData.get("email") as string,      password: formData.get("password") as string,    });    if (res.error) {      setError(res.error.message || "Something went wrong.");    } else {      router.push("/dashboard");    }  }  return (    <main className="max-w-md h-screen flex items-center justify-center flex-col mx-auto p-6 space-y-4 text-white">      <h1 className="text-2xl font-bold">Sign In</h1>    </main>  );}

Add the conditional error message display.

src/app/sign-in/page.tsx
"use client";import { useState } from "react";import { useRouter } from "next/navigation";import { signIn } from "@/lib/auth-client";export default function SignInPage() {  const router = useRouter();  const [error, setError] = useState<string | null>(null);  async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {    e.preventDefault();    setError(null);    const formData = new FormData(e.currentTarget);    const res = await signIn.email({      email: formData.get("email") as string,      password: formData.get("password") as string,    });    if (res.error) {      setError(res.error.message || "Something went wrong.");    } else {      router.push("/dashboard");    }  }  return (    <main className="max-w-md h-screen flex items-center justify-center flex-col mx-auto p-6 space-y-4 text-white">      <h1 className="text-2xl font-bold">Sign In</h1>      {error && <p className="text-red-500">{error}</p>}    </main>  );}

Finally, add the form fields for email and password and a sign-in button.

src/app/sign-in/page.tsx
"use client";import { useState } from "react";import { useRouter } from "next/navigation";import { signIn } from "@/lib/auth-client";export default function SignInPage() {  const router = useRouter();  const [error, setError] = useState<string | null>(null);  async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {    e.preventDefault();    setError(null);    const formData = new FormData(e.currentTarget);    const res = await signIn.email({      email: formData.get("email") as string,      password: formData.get("password") as string,    });    if (res.error) {      setError(res.error.message || "Something went wrong.");    } else {      router.push("/dashboard");    }  }  return (    <main className="max-w-md h-screen flex items-center justify-center flex-col mx-auto p-6 space-y-4 text-white">      <h1 className="text-2xl font-bold">Sign In</h1>      {error && <p className="text-red-500">{error}</p>}      <form onSubmit={handleSubmit} className="space-y-4">        {" "}        <input          name="email"          type="email"          placeholder="Email"          required          className="w-full rounded-md bg-neutral-900 border border-neutral-700 px-3 py-2"        />{" "}        <input          name="password"          type="password"          placeholder="Password"          required          className="w-full rounded-md bg-neutral-900 border border-neutral-700 px-3 py-2"        />{" "}        <button          type="submit"          className="w-full bg-white text-black font-medium rounded-md px-4 py-2 hover:bg-gray-200"        >          {" "}          // [!code ++] Sign In        </button>{" "}      </form>{" "}    </main>  );}

This is the protected page for authenticated users. Start with the basic component in src/app/dashboard/page.tsx.

src/app/dashboard/page.tsx
"use client";

export default function DashboardPage() {
  return (
    <main className="max-w-md h-screen flex items-center justify-center flex-col mx-auto p-6 space-y-4 text-white">
      <h1 className="text-2xl font-bold">Dashboard</h1>
    </main>
  );
}

Import the useSession hook from your Better Auth client. This hook is the key to managing authentication state on the client side. It provides the session data and a pending status.

src/app/dashboard/page.tsx
"use client";import { useRouter } from "next/navigation";import { useSession } from "@/lib/auth-client";export default function DashboardPage() {  const router = useRouter();  const { data: session, isPending } = useSession();  return (    <main className="max-w-md h-screen flex items-center justify-center flex-col mx-auto p-6 space-y-4 text-white">      <h1 className="text-2xl font-bold">Dashboard</h1>    </main>  );}

To protect this route, use a useEffect hook. This effect checks if the session has loaded (!isPending) and if there is no authenticated user (!session?.user). If both are true, it redirects the user to the sign-in page.

src/app/dashboard/page.tsx
"use client";import { useRouter } from "next/navigation";import { useSession } from "@/lib/auth-client";import { useEffect } from "react";export default function DashboardPage() {  const router = useRouter();  const { data: session, isPending } = useSession();  useEffect(() => {    if (!isPending && !session?.user) {      router.push("/sign-in");    }  }, [isPending, session, router]);  return (    <main className="max-w-md h-screen flex items-center justify-center flex-col mx-auto p-6 space-y-4 text-white">      <h1 className="text-2xl font-bold">Dashboard</h1>    </main>  );}

Add loading and redirecting states so the page does not flash an empty dashboard while the session is being verified.

src/app/dashboard/page.tsx
"use client";import { useRouter } from "next/navigation";import { useSession } from "@/lib/auth-client";import { useEffect } from "react";export default function DashboardPage() {  const router = useRouter();  const { data: session, isPending } = useSession();  useEffect(() => {    if (!isPending && !session?.user) {      router.push("/sign-in");    }  }, [isPending, session, router]);  if (isPending)    return <p className="text-center mt-8 text-white">Loading...</p>;  if (!session?.user)    return <p className="text-center mt-8 text-white">Redirecting...</p>;  return (    <main className="max-w-md h-screen flex items-center justify-center flex-col mx-auto p-6 space-y-4 text-white">      <h1 className="text-2xl font-bold">Dashboard</h1>    </main>  );}

Finally, if the user is authenticated, display their name and email from the session object. Also, import the signOut function and add a button that calls it, allowing the user to log out.

src/app/dashboard/page.tsx
"use client";import { useRouter } from "next/navigation";import { useSession, signOut } from "@/lib/auth-client";import { useEffect } from "react";export default function DashboardPage() {  const router = useRouter();  const { data: session, isPending } = useSession();  useEffect(() => {    if (!isPending && !session?.user) {      router.push("/sign-in");    }  }, [isPending, session, router]);  if (isPending) return <p className="text-center mt-8 text-white">Loading...</p>;  if (!session?.user) return <p className="text-center mt-8 text-white">Redirecting...</p>;  const { user } = session;  return (    <main className="max-w-md h-screen flex items-center justify-center flex-col mx-auto p-6 space-y-4 text-white">      <h1 className="text-2xl font-bold">Dashboard</h1>      <p>Welcome, {user.name || "User"}!</p>      <p>Email: {user.email}</p>      <button {/* [!code ++] */}        onClick={() => signOut()} {/* [!code ++] */}        className="w-full bg-white text-black font-medium rounded-md px-4 py-2 hover:bg-gray-200" {/* [!code ++] */}      > {/* [!code ++] */}        Sign Out {/* [!code ++] */}      </button> {/* [!code ++] */}    </main>  );}

Finally, update the home page to link to the sign-in and sign-up pages. Replace the contents of src/app/page.tsx with the following:

src/app/page.tsx
"use client";

import { useRouter } from "next/navigation";

export default function Home() {
  const router = useRouter();

  return (
    <main className="flex items-center justify-center h-screen bg-neutral-950 text-white">
      <div className="flex gap-4">
        <button
          onClick={() => router.push("/sign-up")}
          className="bg-white text-black font-medium px-6 py-2 rounded-md hover:bg-gray-200"
        >
          Sign Up
        </button>
        <button
          onClick={() => router.push("/sign-in")}
          className="border border-white text-white font-medium px-6 py-2 rounded-md hover:bg-neutral-800"
        >
          Sign In
        </button>
      </div>
    </main>
  );
}

Your application is now fully configured.

  1. Start the development server to test it:
bun
bun run dev
pnpm
pnpm run dev
yarn
yarn dev
npm
npm run dev
  1. Navigate to http://localhost:3000 in your browser. You should see the home page with "Sign Up" and "Sign In" buttons.

  2. Click on Sign Up, create a new account, and you should be redirected to the dashboard. You can then sign out and sign back in.

  3. To view the user data directly in your database, you can use Prisma Studio.

bun
bunx prisma studio
pnpm
pnpm prisma studio
yarn
yarn prisma studio
npm
npx prisma studio
  1. This will open a new tab in your browser where you can see the User, Session, and Account tables and their contents.
  • Add support for social login or magic links
  • Implement password reset and email verification
  • Add user profile and account management pages
  • Deploy to Vercel and secure your environment variables
  • Extend your Prisma schema with custom application models
Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu