# better-auth-ts > Better Auth TypeScript/JavaScript authentication library. Use when implementing auth in Next.js, React, Express, or any TypeScript project. Covers email/password, OAuth, JWT, sessions, 2FA, magic links, social login with Next.js 16 proxy.ts patterns. - Author: Naimal Salahuddin - Repository: NaimalArain13/Hackathon-II_The-Evolution-of-Todo - Version: 20260122160836 - Stars: 1 - Forks: 0 - Last Updated: 2026-02-06 - Source: https://github.com/NaimalArain13/Hackathon-II_The-Evolution-of-Todo - Web: https://mule.run/skillshub/@@NaimalArain13/Hackathon-II_The-Evolution-of-Todo~better-auth-ts:20260122160836 --- --- name: better-auth-ts description: Better Auth TypeScript/JavaScript authentication library. Use when implementing auth in Next.js, React, Express, or any TypeScript project. Covers email/password, OAuth, JWT, sessions, 2FA, magic links, social login with Next.js 16 proxy.ts patterns. --- # Better Auth TypeScript Skill Better Auth is a framework-agnostic authentication and authorization library for TypeScript. ## Quick Start ### Installation ```bash # npm npm install better-auth # pnpm pnpm add better-auth # yarn yarn add better-auth # bun bun add better-auth ``` ### Basic Setup See [templates/auth-server.ts](templates/auth-server.ts) for a complete template. ```typescript // lib/auth.ts import { betterAuth } from "better-auth"; export const auth = betterAuth({ database: yourDatabaseAdapter, // See ORM guides below emailAndPassword: { enabled: true }, }); ``` ```typescript // lib/auth-client.ts import { createAuthClient } from "better-auth/client"; export const authClient = createAuthClient({ baseURL: process.env.NEXT_PUBLIC_APP_URL, }); ``` ## ORM Integration (Choose One) **IMPORTANT**: Always use CLI to generate/migrate schema: ```bash npx @better-auth/cli generate # See current schema npx @better-auth/cli migrate # Create/update tables ``` | ORM | Guide | |-----|-------| | **Drizzle** | [reference/drizzle.md](reference/drizzle.md) | | **Prisma** | [reference/prisma.md](reference/prisma.md) | | **Kysely** | [reference/kysely.md](reference/kysely.md) | | **MongoDB** | [reference/mongodb.md](reference/mongodb.md) | | **Direct DB** | Use `pg` Pool directly (see templates) | ## Next.js 16 Integration ### API Route ```typescript // app/api/auth/[...all]/route.ts import { auth } from "@/lib/auth"; import { toNextJsHandler } from "better-auth/next-js"; export const { GET, POST } = toNextJsHandler(auth.handler); ``` ### Proxy (Replaces Middleware) In Next.js 16, `middleware.ts` → `proxy.ts`: ```typescript // proxy.ts import { NextRequest, NextResponse } from "next/server"; import { auth } from "@/lib/auth"; import { headers } from "next/headers"; export async function proxy(request: NextRequest) { const session = await auth.api.getSession({ headers: await headers(), }); if (!session) { return NextResponse.redirect(new URL("/sign-in", request.url)); } return NextResponse.next(); } export const config = { matcher: ["/dashboard/:path*"], }; ``` Migration: `npx @next/codemod@canary middleware-to-proxy .` ### Server Component ```typescript import { auth } from "@/lib/auth"; import { headers } from "next/headers"; import { redirect } from "next/navigation"; export default async function DashboardPage() { const session = await auth.api.getSession({ headers: await headers(), }); if (!session) redirect("/sign-in"); return