Better Auth

1.6.5 · active · verified Sat Apr 18

Better Auth is a comprehensive authentication framework designed for TypeScript applications, supporting various authentication methods like OAuth, OIDC, 2FA, and social logins. The current stable version is 1.6.5, with frequent patch and minor releases, alongside active beta development indicating continuous feature enhancements and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Better Auth in a TypeScript project using the Prisma adapter. It sets up the core `Auth` instance with a placeholder secret and hints at basic session retrieval in a server-side context.

import { Auth } from 'better-auth';
import { PrismaAdapter } from '@better-auth/adapter-prisma';
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

export const auth = new Auth({
  adapter: PrismaAdapter(prisma),
  secret: process.env.AUTH_SECRET ?? 'super-secret-dev-key', // IMPORTANT: Use a strong, unique secret in production
  providers: [], // Configure your authentication providers here
});

// Example usage (e.g., in a Next.js API route):
// import { auth } from './auth'; // Adjust path as needed
// import type { NextApiRequest, NextApiResponse } from 'next';
//
// export default async function handler(req: NextApiRequest, res: NextApiResponse) {
//   const session = await auth.getSession({ req });
//   if (!session) {
//     return res.status(401).json({ message: 'Unauthorized' });
//   }
//   res.status(200).json({ user: session.user });
// }

view raw JSON →