better-auth-best-practices

Configure Better Auth servers, clients, database adapters, sessions, and plugins for TypeScript applications.

Updated Sep 5, 2023
One-click install
npx skills add https://github.com/eyenalxai/dotfiles --skill better-auth-best-practices-eyenalxai
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: better-auth-best-practices
Source: https://github.com/eyenalxai/dotfiles/tree/main/.agents/skills/better-auth-best-practices
Command: npx skills add https://github.com/eyenalxai/dotfiles --skill better-auth-best-practices-eyenalxai

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Setting up TypeScript authentication with Better Auth involves many moving parts—environment variables, database adapters, session strategies, plugins, and CLI migrations—and misconfigurations like using table names instead of model names cause subtle failures. This Skill provides version-aware guidance and a structured setup workflow to configure Better Auth correctly. ## Core Features & Use Cases - Version-Matched Documentation: Resolves the installed better-auth version from the lockfile and fetches matching docs via MCP or llms.txt, avoiding outdated API guidance. - Complete Setup Workflow: Covers installation, environment variables, auth.ts creation, route handlers, and migrations for built-in, Drizzle, and Prisma adapters. - Configuration Reference: Details session storage strategies, cookie cache modes, hooks, plugins, rate limiting, and common gotchas like model name mapping. - Use Case: When adding email/password and Google OAuth login to a Next.js app with a Drizzle/Postgres database, use this Skill to generate the auth config, run schema generation, and wire up the client with correct plugin imports. ## Quick Start Set up Better Auth in my TypeScript project with email/password authentication and a PostgreSQL database using the Drizzle adapter.

Frequently Asked Questions about better-auth-best-practices

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up Better Auth with a database in TypeScript?

Install better-auth, set BETTER_AUTH_SECRET and BETTER_AUTH_URL, create an auth.ts with your database config, and add a route handler. Run npx auth@latest migrate for the built-in adapter, or npx auth@latest generate followed by drizzle-kit or prisma migrate for ORM adapters.

How to run Better Auth migrations with Drizzle or Prisma?

Run npx auth@latest generate to output the schema file, then apply it with drizzle-kit push (dev) or drizzle-kit generate plus migrate (prod) for Drizzle, or prisma migrate dev for Prisma. Re-run the CLI whenever you add or change plugins.

Does Better Auth support Drizzle and Prisma adapters?

Yes, Better Auth provides ORM adapters imported from better-auth/adapters/drizzle, better-auth/adapters/prisma, and better-auth/adapters/mongodb. It also accepts direct connections like pg.Pool, mysql2, better-sqlite3, and postgres.js.

Why does Better Auth fail when I use my database table name?

Better Auth configuration uses the ORM model name, not the underlying table name. If your Prisma model is User mapping to a users table, set modelName to "user" rather than "users".

Where does Better Auth store sessions by default?

If secondaryStorage like Redis is defined, sessions go there instead of the database. Set session.storeSessionInDatabase to true to also persist to the database, or use cookieCache for fully stateless sessions without a database.

How do I import Better Auth plugins correctly?

Import plugins from dedicated paths like better-auth/plugins/two-factor rather than the barrel better-auth/plugins export to enable tree-shaking. Client plugins are passed separately to createAuthClient.