better-auth
Skill4 starsChanged 4 months agoMITView on GitHub
Better Auth 1.6 for Next.js 16 (Lucia's 2026 successor): email/password, OAuth, magic links, passkeys, 2FA, organizations. Edge-native; pairs with Drizzle + Neon.
- Reads credentials
- Installs packages
8 other projects use a version of this.
Adopted by
Most recently taken up by
- stonegiantstudio/skillstheir own version, first seen Sep 29, 2026
- VoDaiLocz/kilo-kit-mcptheir own version, first seen Aug 21, 2026
- duongthinh03/portfoliotheir own version, first seen Jun 24, 2026
- georgekhananaev/claude-skills-vaulttheir own version, first seen May 25, 2026
- truongtv22/theone-training-skillstheir own version, first seen Nov 19, 2025
- akornmeier/claude-configtheir own version, first seen Nov 16, 2025
Other versions8 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 8
- mrgoonie/claudekit-skills★ 2.2ksame name
- duongthinh03/portfolio★ 85same name
- georgekhananaev/claude-skills-vault★ 28same name
- VoDaiLocz/kilo-kit-mcp★ 27same name
- akornmeier/claude-config★ 4same name
- stonegiantstudio/skills★ 2same name
- truongtv22/theone-training-skills★ 1same name
- Linhv14/claude-skill★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository162 other files it gives its agents.
- agents/AGENTS.mdAGENTS.md
- skills/compound-engineering/AGENTS.mdAGENTS.md
- skills/trailofbits-security/AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- examples/CLAUDE.mdCLAUDE.md
- skills/compound-engineering/CLAUDE.mdCLAUDE.md
- skills/trailofbits-security/CLAUDE.mdCLAUDE.md
- app-store-reviewSkill
- advanced-javascriptSkill
- ai-native-uiSkill
- anime-jsSkill
- app-l100Skill
- barba-jsSkill
- brand-guidelinesSkill
- cinematic-web-engineSkill
- codebase-knowledge-graphSkill
- crossplatform-monorepoSkill
- css-first-uiSkill
- design-auditSkill
- design-critiqueSkill
- dev-cycleSkill
- device-hardware-bridgeSkill
- discovery-docSkill
- docxSkill
- edge-intelligenceSkill
- graph-aware-reviewSkill
- linkedin-posterSkill
- local-first-architectureSkill
- modern-build-pipelineSkill
- motion-nativeSkill
- nano-bananaSkill
- playwrightSkill
- pptxSkill
- project-initSkill
- remotionSkill
- sensory-nativeSkill
- spline-3dSkill
- streaming-cacheSkill
- tanstack-ecosystemSkill
- design-taste-frontendSkill
- tauri-desktopSkill
- threejsSkill
- transition-nativeSkill
- ui-design-stackSkill
- visual-nativeSkill
- web-l100Skill
- web-platform-apisSkill
- xlsxSkill
- claude-real-videoSkill
- clone-websiteSkill
- css-keyframe-animationsSkill
- design-checkSkill
- design-dnaSkill
- design-polishSkill
- drizzle-neonSkill
- fastlaneSkill
- angular-architectSkill
- api-designerSkill
- architecture-designerSkill
- atlassian-mcpSkill
- The first 60 shown.
The file
Files
Better Auth for Next.js 16
The recommended auth library for new TypeScript projects in 2026 — replaces both Lucia (officially sunset by its maintainer, who redirected users to better-auth) and NextAuth/Auth.js for greenfield apps. Plugin-based, ships only what you import, designed from day one for edge runtimes (Vercel Edge, Cloudflare Workers, Bun).
Version pairing (verified 2026-07-04 via npm view)
| Package | Version | Notes |
better-auth | ^1.6 | Core + email/password + sessions. Stable line (1.6.23 as of 2026-07-04). |
drizzle-orm | ^0.45 | DB adapter (Drizzle is recommended). Alternatives: @better-auth/prisma, @better-auth/mongoose |
@better-auth/cli (optional) | latest | Schema generator: writes Drizzle schema from your auth config |
1.7 is in RC (announced 2026-06-26; npm stable is still 1.6.x) and is breaking: MCP support moves to
@better-auth/mcp,validAudiences→ protected resources, the deprecatedoidcProviderplugin is removed, stricter PKCE, proxy-header trust off by default. New in 1.7: DPoP-bound tokens, OIDC back-channel logout, built-in i18n, a Drizzle-v1-relations adapter. Stay on 1.6 until 1.7 GA, then follow the 1.7 upgrade guide.
Install
npm install better-auth
# DB driver: pick one (matches your existing setup)
npm install drizzle-orm @neondatabase/serverlessAdd to .env.local:
BETTER_AUTH_SECRET="$(openssl rand -base64 32)"
BETTER_AUTH_URL=http://localhost:3000
# OAuth (only what you actually use)
GITHUB_CLIENT_ID=...
GITHUB_CLIENT_SECRET=...Server setup
lib/auth.ts — the canonical config:
import { betterAuth } from 'better-auth';
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
import { db } from '@/db';
import * as schema from '@/db/auth-schema';
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: 'pg',
schema,
}),
emailAndPassword: {
enabled: true,
requireEmailVerification: false, // set true once you wire up email sender
},
socialProviders: {
github: {
clientId: process.env.GITHUB_CLIENT_ID!,
clientSecret: process.env.GITHUB_CLIENT_SECRET!,
},
},
session: {
expiresIn: 60 * 60 * 24 * 7, // 7 days
cookieCache: { enabled: true, maxAge: 60 * 5 }, // 5min in-memory cache, big perf win
},
});
export type Session = typeof auth.$Infer.Session;DB schema (auto-generated)
npx @better-auth/cli generateThis writes db/auth-schema.ts with user, session, account, verification tables typed for Drizzle. Commit it.
Then push to your DB:
npx drizzle-kit push # dev
# or: npx drizzle-kit generate && npx drizzle-kit migrate # prodRoute handler — mounts auth endpoints
app/api/auth/[...all]/route.ts
import { auth } from '@/lib/auth';
import { toNextJsHandler } from 'better-auth/next-js';
export const { GET, POST } = toNextJsHandler(auth);This single file exposes:
POST /api/auth/sign-up/emailPOST /api/auth/sign-in/emailPOST /api/auth/sign-outGET /api/auth/get-sessionGET /api/auth/callback/github- (and every other endpoint from enabled plugins)
Client setup
lib/auth-client.ts
import { createAuthClient } from 'better-auth/react';
export const authClient = createAuthClient({
baseURL: process.env.NEXT_PUBLIC_AUTH_URL ?? 'http://localhost:3000',
});
export const { signIn, signUp, signOut, useSession } = authClient;Sign-up / Sign-in components
'use client';
import { signUp, signIn } from '@/lib/auth-client';
import { useState } from 'react';
import { useRouter } from 'next/navigation';
export function SignUpForm() {
const router = useRouter();
const [error, setError] = useState<string>();
async function action(formData: FormData) {
const { error } = await signUp.email({
email: formData.get('email') as string,
password: formData.get('password') as string,
name: formData.get('name') as string,
});
if (error) return setError(error.message);
router.push('/dashboard');
}
return (
<form action={action} className="space-y-3">
<input name="name" required className="w-full rounded border px-3 py-2" placeholder="Name" />
<input name="email" type="email" required className="w-full rounded border px-3 py-2" placeholder="Email" />
<input name="password" type="password" required className="w-full rounded border px-3 py-2" placeholder="Password" />
{error && <p className="text-sm text-red-600">{error}</p>}
<button className="w-full rounded bg-zinc-900 px-3 py-2 text-white">Sign up</button>
</form>
);
}OAuth (one button):
'use client';
import { signIn } from '@/lib/auth-client';
export function GitHubButton() {
return (
<button onClick={() => signIn.social({ provider: 'github', callbackURL: '/dashboard' })}>
Continue with GitHub
</button>
);
}Reading the session
In a Server Component (most common)
import { auth } from '@/lib/auth';
import { headers } from 'next/headers';
import { redirect } from 'next/navigation';
export default async function Dashboard() {
const session = await auth.api.getSession({ headers: await headers() });
if (!session) redirect('/sign-in');
return <h1>Hello {session.user.name}</h1>;
}In a Client Component (reactive)
'use client';
import { useSession } from '@/lib/auth-client';
export function UserMenu() {
const { data: session, isPending } = useSession();
if (isPending) return <div>…</div>;
if (!session) return <a href="/sign-in">Sign in</a>;
return <div>{session.user.email}</div>;
}In a Route Handler / Server Action
import { auth } from '@/lib/auth';
import { headers } from 'next/headers';
export async function POST(req: Request) {
const session = await auth.api.getSession({ headers: req.headers });
if (!session) return new Response('Unauthorized', { status: 401 });
// …
}Middleware — protecting routes
middleware.ts (Next.js root)
import { NextResponse, type NextRequest } from 'next/server';
import { getSessionCookie } from 'better-auth/cookies';
export async function middleware(req: NextRequest) {
const sessionCookie = getSessionCookie(req);
if (!sessionCookie && req.nextUrl.pathname.startsWith('/dashboard')) {
return NextResponse.redirect(new URL('/sign-in', req.url));
}
return NextResponse.next();
}
export const config = {
matcher: ['/dashboard/:path*'],
};Middleware only checks for cookie presence (fast — no DB hit). The actual session validation happens inside the route via auth.api.getSession().
Note: in Next.js 16, the root middleware file moved from
middleware.ts→proxy.ts(with the same API). Use whichever your project is on; thenext-upgradecodemod handles the rename.
Plugins (opt-in features)
import { twoFactor, organization, magicLink, passkey } from 'better-auth/plugins';
export const auth = betterAuth({
// ... base config
plugins: [
twoFactor(), // TOTP 2FA
magicLink({ sendMagicLink: async ({ email, url }) => sendEmail({ to: email, body: url }) }),
passkey(), // WebAuthn / Touch ID / Face ID
organization({ allowUserToCreateOrganization: true }),
],
});Each plugin auto-extends the schema (re-run npx @better-auth/cli generate after adding) and exposes its own endpoints.
Common bugs
| Symptom | Cause | Fix |
BETTER_AUTH_SECRET is required at runtime | Env not set in Vercel | Add BETTER_AUTH_SECRET to project env on Vercel dashboard |
| OAuth callback URL mismatch | Forgot to register https://yourapp.vercel.app/api/auth/callback/github in the OAuth app | Add the exact callback URL in GitHub/Google OAuth settings |
Cookie not set after sign-in in prod | Cookie domain mismatch | Set BETTER_AUTH_URL to your actual production URL (no trailing slash) |
Cannot find module 'better-auth/next-js' | Old version | npm install better-auth@latest (^1.6) |
Session always null in Server Component | Forgot await headers() | Next 16 requires async — headers: await headers() |
| Drizzle adapter complains about missing tables | Didn't push schema | Run npx drizzle-kit push after cli generate |
When NOT to use Better Auth
- Existing NextAuth/Auth.js project that works fine → don't migrate (NextAuth v5 is solid; better-auth's edge is mainly for greenfield)
- Need enterprise SSO / SAML / SCIM out of the box → Clerk or WorkOS will save you weeks
- "Just sign in with Google" and nothing else, ever → NextAuth v5 is 5 lines and zero schema work
When to choose Better Auth
- New project on Next.js + Drizzle + Neon (the user's primary stack)
- You want email/password + OAuth + 2FA without piecing together libraries
- You're targeting edge runtimes
- You want a typed session and don't want vendor lock-in
Sources
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

