К основному содержанию
скилл CX Codex

better-auth · giuseppe-trisciuoglio — скилл для Claude Code: вход в NestJS и Next.js

Скилл с готовыми шаблонами Better Auth: подключение к NestJS, Drizzle ORM и PostgreSQL, вход через соцсети и плагины вроде 2FA и passkey.

Установить в Claude Code · Через Skills CLI (Vercel) Все способы и платформы ↓
npx skills add giuseppe-trisciuoglio/developer-kit --skill better-auth -a claude-code
Открыть на GitHub

Что делает better-auth

Скилл описывает четыре шага: база, бэкенд, фронтенд, плагины. На первом шаге ставятся drizzle-orm, pg, @auth/drizzle-adapter и better-auth, создаётся drizzle.config.ts, затем npx drizzle-kit generate и npx drizzle-kit migrate. Проверить результат можно через psql $DATABASE_URL -c "\dt" — должны появиться таблицы user, account, session, verification_token.

Дальше собирается экземпляр betterAuth с drizzleAdapter и контроллер NestJS с обработчиком auth.handler. На фронтенде Next.js App Router создаётся клиент через createAuthClient и middleware, который уводит неавторизованных с /dashboard на /sign-in. Для соцвхода нужны AUTH_GITHUB_CLIENT_ID и AUTH_GITHUB_CLIENT_SECRET в переменных окружения — без них провайдер не поднимется.

Плагины добавляются на четвёртом шаге: twoFactor, passkey, organization, magicLink, sso. После каждого нового плагина миграции нужно прогнать заново, иначе таблиц под него в базе не будет. Скилл работает с TypeScript-стеком; если у вас другой ORM или бэкенд не на NestJS, шаблоны придётся переписывать.

Какие задачи решает

  • поднять вход по email и паролю в NestJS с сессиями
  • настроить схему Drizzle под PostgreSQL и прогнать миграции
  • добавить вход через GitHub или Google
  • включить 2FA с TOTP и доверенными устройствами
  • закрыть маршруты /dashboard через middleware Next.js

Примеры запросов агенту

  • Настрой Better Auth в NestJS с Drizzle и PostgreSQL по шагам из скилла
  • Добавь вход через GitHub и страницу /sign-in в Next.js App Router
  • Подключи плагин 2FA с TOTP и доверенными устройствами

Как установить better-auth

1Через Skills CLI (Vercel)

npx skills add giuseppe-trisciuoglio/developer-kit --skill better-auth -a claude-code

2Вручную: положить файлы

~/.claude/skills/better-auth/SKILL.md

Папка скилла целиком; для одного проекта — .claude/skills/better-auth/

По шагам, с командами для каждой платформы: Как установить скилл в Claude Code: пять способов

Вопросы о better-auth

Как установить better-auth в Claude?

Выполните npx skills add giuseppe-trisciuoglio/developer-kit --skill better-auth -a claude-code. Скилл появится в ~/.claude/skills/better-auth/SKILL.md.

Что делает скилл better-auth?

Даёт шаблоны подключения Better Auth: схема Drizzle для PostgreSQL, экземпляр betterAuth в NestJS, клиент и middleware в Next.js, а также плагины 2FA, passkey, organization, magic link и SSO.

Что нужно для работы скилла?

TypeScript-проект с NestJS на бэкенде и Next.js App Router на фронтенде, PostgreSQL и Drizzle ORM. Для соцвхода понадобятся AUTH_GITHUB_CLIENT_ID и AUTH_GITHUB_CLIENT_SECRET.

Исходный текст

SKILL.md · MIT
---
name: better-auth
description: Provides Better Auth integration patterns for NestJS backend and Next.js frontend with Drizzle ORM and PostgreSQL. Use when setting up Better Auth with NestJS backend, integrating Next.js App Router frontend, configuring Drizzle ORM schema, implementing social login (GitHub, Google), adding plugins (2FA, Organization, SSO, Magic Link, Passkey), implementing email/password authentication with session management, or creating protected routes and middleware.
allowed-tools: Read, Write, Edit, Glob, Grep, Bash
---

# Better Auth Integration Guide

## Overview

Better Auth is a type-safe authentication framework for TypeScript supporting multiple providers, 2FA, SSO, organizations, and passkeys. This skill covers integration patterns for NestJS backend with Drizzle ORM + PostgreSQL and Next.js App Router frontend.

## When to Use

- Setting up Better Auth with NestJS backend
- Integrating Next.js App Router frontend
- Configuring Drizzle ORM schema with PostgreSQL
- Implementing social login (GitHub, Google, Facebook, Microsoft)
- Adding MFA/2FA with TOTP, passkey passwordless auth, or magic links
- Managing trusted devices and backup codes for account recovery
- Building multi-tenant apps with organizations or SSO
- Creating protected routes with session management

## Quick Start

### Installation

```bash
# Backend (NestJS)
npm install better-auth @auth/drizzle-adapter drizzle-orm pg
npm install -D drizzle-kit

# Frontend (Next.js)
npm install better-auth
```

### 4-Phase Setup

1. **Database**: Install Drizzle, configure schema, run migrations
2. **Backend**: Create Better Auth instance with NestJS module
3. **Frontend**: Configure auth client, create pages, add middleware
4. **Plugins**: Add 2FA, passkey, organizations as needed

See `references/nestjs-setup.md` for complete backend setup, `references/plugins.md` for plugin configuration.

## Instructions

### Phase 1: Database Setup

1. **Install dependencies**
   ```bash
   npm install drizzle-orm pg @auth/drizzle-adapter better-auth
   npm install -D drizzle-kit
   ```

2. **Create Drizzle config** (`drizzle.config.ts`)
   ```typescript
   import { defineConfig } from 'drizzle-kit';
   export default defineConfig({
     schema: './src/auth/schema.ts',
     out: './drizzle',
     dialect: 'postgresql',
     dbCredentials: { url: process.env.DATABASE_URL! },
   });
   ```

3. **Generate and run migrations**
   ```bash
   npx drizzle-kit generate
   npx drizzle-kit migrate
   ```

   **Checkpoint**: Verify tables created: `psql $DATABASE_URL -c "\dt"` should show `user`, `account`, `session`, `verification_token` tables.

### Phase 2: Backend Setup (NestJS)

1. **Create database module** - Set up Drizzle connection service

2. **Configure Better Auth instance**
   ```typescript
   // src/auth/auth.instance.ts
   import { betterAuth } from 'better-auth';
   import { drizzleAdapter } from '@auth/drizzle-adapter';
   import * as schema from './schema';

   export const auth = betterAuth({
     database: drizzleAdapter(schema, { provider: 'postgresql' }),
     emailAndPassword: { enabled: true },
     socialProviders: {
       github: {
         clientId: process.env.AUTH_GITHUB_CLIENT_ID!,
         clientSecret: process.env.AUTH_GITHUB_CLIENT_SECRET!,
       }
     }
   });
   ```

3. **Create auth controller**
   ```typescript
   @Controller('auth')
   export class AuthController {
     @All('*')
     async handleAuth(@Req() req: Request, @Res() res: Response) {
       return auth.handler(req);
     }
   }
   ```

   **Checkpoint**: Test endpoint `GET /auth/get-session` returns `{ session: null }` when unauthenticated (no error).

### Phase 3: Frontend Setup (Next.js)

1. **Configure auth client** (`lib/auth.ts`)
   ```typescript
   import { createAuthClient } from 'better-auth/client';
   export const authClient = createAuthClient({
     baseURL: process.env.NEXT_PUBLIC_APP_URL!
   });
   ```

2. **Add middleware** (`middleware.ts`)
   ```typescript
   import { auth } from '@/lib/auth';
   export default auth((req) => {
     if (!req.auth && req.nextUrl.pathname.startsWith('/dashboard')) {
       return Response.redirect(new URL('/sign-in', req.nextUrl.origin));
     }
   });
   export const config = { matcher: ['/dashboard/:path*'] };
   ```

3. **Create sign-in page** with form or social buttons

   **Checkpoint**: Navigating to `/dashboard` when logged out should redirect to `/sign-in`.

### Phase 4: Advanced Features

Add plugins from `references/plugins.md`:

- **2FA**: `twoFactor({ issuer: 'AppName', otpOptions: { sendOTP } })`
- **Passkey**: `passkey({ rpID: 'domain.com', rpName: 'App' })`
- **Organizations**: `organization({ avatar: { enabled: true } })`
- **Magic Link**: `magicLink({ sendMagicLink })`
- **SSO**: `sso({ saml: { enabled: true } })`

   **Checkpoint**: After adding plugins, re-run migrations and verify new tables exist.

## Examples

### Example 1: Server Component with Session

**Input**: Display user data in a Next.js Server Component.

```tsx
// app/dashboard/page.tsx
import { auth } from '@/lib/auth';
import { redirect } from 'next/navigation';

export default async function DashboardPage() {
  const session = await auth();

  if (!session) {
    redirect('/sign-in');
  }

  return (
    <div>
      <h1>Welcome, {session.user.name}</h1>
      <p>Email: {session.user.email}</p>
    </div>
  );
}
```

**Output**: Renders user info for authenticated users; redirects unauthenticated to sign-in.

### Example 2: 2FA TOTP Verification with Trusted Device

**Input**: User has 2FA enabled and wants to sign in, marking device as trusted.

```typescript
// Server: Configure 2FA with OTP sending
export const auth = betterAuth({
  plugins: [
    twoFactor({
      issuer: 'MyApp',
      otpOptions: {
        async sendOTP({ user, otp }, ctx) {
          await sendEmail({
            to: user.email,
            subject: 'Your verification code',
            body: `Code: ${otp}`
          });
        }
      }
    })
  ]
});

// Client: Verify TOTP and trust device
const verify2FA = async (code: string) => {
  const { data } = await authClient.twoFactor.verifyTotp({
    code,
    trustDevice: true  // Device trusted for 30 days
  });

  if (data) {
    router.push('/dashboard');
  }
};
```

**Output**: User authenticated; device trusted for 30 days without 2FA prompt.

### Example 3: Passkey Registration and Login

**Input**: Enable passkey (WebAuthn) authentication for passwordless login.

```typescript
// Server
import { passkey } from '@better-auth/passkey';
export const auth = betterAuth({
  plugins: [
    passkey({
      rpID: 'example.com',
      rpName: 'My App',
    })
  ]
});

// Client: Register passkey
const registerPasskey = async () => {
  const { data } = await authClient.passkey.register({
    name: 'My Device'
  });
};

// Client: Sign in with autofill
const signInWithPasskey = async () => {
  await authClient.signIn.passkey({
    autoFill: true,  // Browser suggests passkey
  });
};
```

**Output**: Users can register and authenticate with biometrics, PIN, or security keys.

For more examples (backup codes, organizations, magic link, conditional UI), see `references/plugins.md` and `references/passkey.md`.

## Best Practices

1. **Environment Variables**: Store all secrets in `.env`, add to `.gitignore`
2. **Secret Generation**: Use `openssl rand -base64 32` for `BETTER_AUTH_SECRET`
3. **HTTPS Required**: OAuth callbacks need HTTPS (use `ngrok` for local testing)
4. **Session Expiration**: Configure based on security requirements (7 days default)
5. **Database Indexing**: Add indexes on `email`, `userId` for performance
6. **Error Handling**: Return generic errors without exposing sensitive details
7. **Rate Limiting**: Add to auth endpoints to prevent brute force attacks
8. **Type Safety**: Use `npx better-auth typegen` for full TypeScript coverage

## Constraints and Warnings

### Security Notes

- **Never commit secrets**: Add `.env` to `.gitignore`; never commit OAuth secrets or DB credentials
- **Validate redirect URLs**: Always validate OAuth redirect URLs to prevent open redirects
- **Hash passwords**: Better Auth handles password hashing automatically; never implement custom hashing
- **Session storage**: For production, use Redis or another scalable session store
- **HTTPS Only**: Always use HTTPS for authentication in production
- **Email Verification**: Always implement email verification for password-based auth

### Known Limitations

- Better Auth requires Node.js 18+ for Next.js App Router support
- Some OAuth providers require specific redirect URL formats
- Passkeys require HTTPS and compatible browsers
- Organization features require additional database tables

## Resources

### Documentation

- [Better Auth](https://www.better-auth.com) - Official documentation
- [Drizzle ORM](https://orm.drizzle.team) - Database ORM
- [NestJS](https://docs.nestjs.com) - Backend framework
- [Next.js](https://nextjs.org/docs/app) - Frontend framework

### Reference Implementations

- `references/nestjs-setup.md` - Complete NestJS backend setup
- `references/nextjs-setup.md` - Complete Next.js frontend setup
- `references/plugins.md` - Plugin configuration (2FA, passkey, organizations, SSO, magic link)
- `references/mfa-2fa.md` - Detailed MFA/2FA guide
- `references/passkey.md` - Detailed passkey implementation
- `references/schema.md` - Drizzle schema reference
- `references/social-providers.md` - Social provider configuration

© giuseppe-trisciuoglio · MIT · giuseppe-trisciuoglio/developer-kit

Похожие скиллы

Всё по теме «Бэкенд и API» →

Сначала — better-auth от других авторов.

better-auth mrgoonie Скилл проводит агента по подключению Better Auth: email-пароли, OAuth, 2FA, passkeys и роли. Пригодится, когда в проект нужно встроить вход и сессии. CX Codex +1 390установок safe-action-better-auth next-safe-action Скилл подключает Better Auth к next-safe-action: middleware сам достаёт сессию, режет неавторизованные запросы и отдаёт типизированный контекст. CX Codex +1 477установок nuxt-better-auth onmax Направляет настройку аутентификации Nuxt через @nuxtjs/better-auth: от сессий и маршрутов до серверных API и плагинов. CX Codex +1 2 000установок better-auth-authentication bobmatnyc Скилл для TypeScript-приложений: помогает настроить вход по email и паролю, социальные провайдеры и проверку почты. CX Codex +1 389установок better-auth-core bobmatnyc Скилл для настройки Better Auth в TypeScript-приложениях: инстанс, серверные обработчики, клиент и сессии. Пригодится, когда нужно подключить аутентификацию без долгих поисков по докам. CX Codex +1 358установок better-auth-security-best-practices better-auth Настраивает защиту Better Auth: ограничивает частоту запросов, проверяет происхождение, шифрует токены и защищает пользовательские сессии. CX Codex +1 48 000установок create-auth better-auth Скилл проводит агента через планирование и настройку аутентификации в TypeScript/JavaScript-проекте на Better Auth: от сканирования кода до страниц входа. CX Codex +1 13 400установок

Из того же набора93

langchain4j-tool-function-calling-patterns Скилл подсказывает агенту, как описывать Java-методы через @Tool, регистрировать их в AiServices и обрабатывать ошибки вызова. CX Codex +1 3 700установок unit-test-security-authorization Скилл задаёт шаблоны юнит-тестов авторизации Spring Security: @PreAuthorize, @Secured, @RolesAllowed и свои permission evaluators. Пригодится, когда нужно проверить доступ по ролям без полного контекста Spring Security. CX Codex +1 3 700установок spring-data-neo4j Настраивает работу со Spring Data Neo4j в проектах на Spring Boot. Помогает писать Cypher-запросы, маппить графовые сущности и готовить тесты с Neo4j Harness. CX Codex +1 3 800установок sonarqube-mcp Проверяет quality gate, ищет проблемы SonarQube и SonarCloud, разбирает правила до отправки кода. Нужен для проверки проекта или pull request в работе агента. CX Codex +1 3 600установок aws-lambda-typescript-integration Помогает проектировать серверлесс-функции на TypeScript под AWS Lambda, выбирая между NestJS и чистым кодом с оптимизацией холодного старта. CX Codex +1 3 600установок aws-lambda-python-integration Подсказывает агенту, как писать и разворачивать Python-функции в AWS Lambda: Chalice или чистый Python, холодный старт, API Gateway и ALB. CX Codex +1 3 600установок graalvm-native-image Скилл помогает компилировать Java-приложения в нативные бинарники с помощью GraalVM Native Image. Пригодится для сокращения времени холодного старта и потребления памяти. CX Codex +1 3 500установок memory-md-management Скилл проверяет файлы памяти проекта вроде CLAUDE.md, ставит им оценку по шести критериям и предлагает точечные правки. CX Codex +1 3 500установок

Спонсоры проекта

Компании, которые помогают держать аналитику открытой