Home › Auth (Better Auth) › Better Auth authentication Cursor rules

Better Auth authentication Cursor rules

Cursor rules for TypeScript apps using Better Auth: one server instance, DB adapter and migrations, session checks on the server, plugins, and safe secrets.

CursorAuth (Better Auth)

How to use

Save this as .cursor/rules/better-auth.mdc in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

Note: Review and test before use. Provided as is, without warranty. See Terms.

.cursor/rules/better-auth.mdcDownload  
---
description: Better Auth conventions
globs: ["src/**/*.{ts,tsx}", "lib/auth*.ts", "app/**/*.{ts,tsx}"]
alwaysApply: false
---

# Better Auth

- One server instance in `lib/auth.ts`: `export const auth = betterAuth({ database: <adapter>, emailAndPassword: {...}, socialProviders: {...}, plugins: [...] })`. One client in `lib/auth-client.ts` via `createAuthClient()` from the framework entry (`better-auth/react`, `/vue`, `/svelte`).
- Secrets from env only: `BETTER_AUTH_SECRET`, `BETTER_AUTH_URL`, provider client IDs/secrets. Never hard-code or log them, and never import `lib/auth.ts` into client components.
- Mount the handler once (e.g. Next.js `app/api/auth/[...all]/route.ts` with `toNextJsHandler(auth)`; Hono/Express with `auth.handler`).
- Schema: after adding plugins or fields run the CLI (`npx @better-auth/cli generate` for ORM schemas, or `migrate` for the built-in Kysely adapter), then the ORM's migration. Don't hand-edit auth tables.
- Authorization is enforced on the server: `const session = await auth.api.getSession({ headers })` in route handlers, server actions and loaders; return 401/redirect when missing. Middleware cookie checks are only an optimistic UX redirect, not security.
- Check ownership/roles for every resource access (e.g. with the `admin` or `organization` plugin roles); a valid session isn't permission.
- Add plugins (twoFactor, passkey, organization, magicLink) on both server and client (`...Client()` plugin) so types line up.
- Configure `trustedOrigins` for every frontend origin; keep cookies `secure` in production.
- Rate limiting stays enabled for sign-in, sign-up and password reset; send verification and reset emails from a server-side mailer.

## Done when
Typecheck and build pass, migrations are generated and committed, and sign-up, sign-in, sign-out and a protected route work end to end.

More: all Auth (Better Auth) rules · all Cursor rules

Related rules

Cloudflare Agents SDK rules

Cursor rules for stateful AI agents on Cloudflare (agents package): Durable Object state, SQL, scheduling, and WebSockets.

CursorCloudflare Workers / Hono

Django Ninja API rules

Cursor rules for Django Ninja APIs on Django 5.2: routers, Pydantic schemas, auth, async views, and tests.

CursorDjango

Django REST Framework rules

Cursor rules for Django 5 + Django REST Framework APIs: serializers, viewsets, permissions, and query performance.

CursorDjango

Fastify 5 API rules

Cursor rules for Fastify 5 APIs in TypeScript: plugins and encapsulation, JSON schema validation, hooks, and inject tests.

CursorNode.js / TypeScript

Go gRPC service rules

Cursor rules for gRPC services in Go with protobuf, buf for codegen and linting, interceptors, and status errors.

CursorGo

GraphQL server (Apollo) rules

Cursor rules for schema-first GraphQL servers with Apollo Server, codegen types, DataLoader, and safe errors.

CursorGraphQL

Get the best new AI prompts every week

One short email a week: the best new prompts and rules for ChatGPT, Claude, Gemini, Midjourney, Cursor and more, plus what changed in the tools. Free, unsubscribe anytime.

Finishes on our newsletter page at beehiiv (opens in a new tab). Or subscribe directly.