This guide shows how to set up Better Auth with Prisma and a database (SQLite or Postgres) in a Next.js project.
# Core dependencies
pnpm add better-auth @prisma/client
# Dev dependencies
pnpm add -D prisma
# add auth secret in .env
BETTER_AUTH_SECRET=KfUQ5cyOUARB4x1y9R8Af5QYq1H5ZcCr
# add auth base url in .env
BETTER_AUTH_URL=http://localhost:3000 # Base URL of your app
# Initialize Prisma
npx prisma init
npx prisma generate # this will install prisma, @prisma/client and generate Prisma ClientDATABASE_URL="file:./dev.db" for sqlite
and DATABASE_URL="postgresql://username:password@localhost:5432/dbname?schema=public" for postgres
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
// If your Prisma file is located elsewhere, you can change the path
import { PrismaClient } from "@/generated/prisma";
const prisma = new PrismaClient();
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "sqlite", // or "mysql", "postgresql", ...etc
}),
});npx @better-auth/cli generateimport { betterAuth } from "better-auth";
export const auth = betterAuth({
//...other options
emailAndPassword: {
enabled: true,
},
socialProviders: {
github: {
clientId: process.env.GITHUB_CLIENT_ID as string,
clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
},
},
});import { auth } from "@/lib/auth"; // path to your auth file
import { toNextJsHandler } from "better-auth/next-js";
export const { POST, GET } = toNextJsHandler(auth);import { createAuthClient } from "better-auth/react"
export const authClient = createAuthClient({
/** The base URL of the server (optional if you're using the same domain) */
baseURL: "http://localhost:3000"
})
export const { signIn, signUp, useSession } = createAuthClient()