# safe-action-better-auth

This skill wires Better Auth into next-safe-action: the middleware fetches the session, rejects unauthenticated calls and hands you a typed context.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode · 477 installs · ★ 1
- Author: next-safe-action
- License: not stated · policy=link-only · description only; the file stays with the author
- Source: https://github.com/next-safe-action/skills/tree/HEAD/skills/safe-action-better-auth/SKILL.md
- Updated: 2026-09-03
- HTML: https://affpapa.org/en/nevibecode/skills/safe-action-better-auth

## What it does

Reach for it when a Next.js project already uses next-safe-action and you are adding sign-in or permission checks to server actions. The skill walks through building an action client with the `betterAuth(auth)` middleware: it calls `auth.api.getSession()` with headers from `next/headers`, and when there is no session it triggers `unauthorized()` from `next/navigation`. Your action context gets `{ auth: { user, session } }`, with types inferred from your Better Auth instance, plugin fields included.

The steps cover a Better Auth instance with `nextCookies()` last in the plugin array, the `authInterrupts: true` flag in `next.config.ts`, a separate client for authenticated actions, and a sample action with `inputSchema`. There is a section on custom `authorize` callbacks plus the `BetterAuthContext`, `AuthorizeFn` and `BetterAuthOpts` types.

Two spots where things break quietly. Skip `nextCookies()` and the session comes back null whenever an action tries to set cookies, so check the plugin order first. Skip the `authInterrupts` flag and `unauthorized()` throws at runtime. Also, don't refetch the session inside `authorize` — it arrives as `authData`.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add next-safe-action/skills --skill safe-action-better-auth -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/safe-action-better-auth/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/safe-action-better-auth/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add next-safe-action/skills --skill safe-action-better-auth -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/safe-action-better-auth/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add next-safe-action/skills --skill safe-action-better-auth -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/safe-action-better-auth/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install next-safe-action/skills safe-action-better-auth
```

2. **Manually: put the files in place**
- File: `.github/skills/safe-action-better-auth/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add next-safe-action/skills --skill safe-action-better-auth -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/next-safe-action/skills --path skills/safe-action-better-auth
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/safe-action-better-auth/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add next-safe-action/skills --skill safe-action-better-auth -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/next-safe-action/skills/tree/HEAD/skills/safe-action-better-auth/SKILL.md

## When to use it
- block unauthenticated calls to server actions
- get typed `user` and `session` in the action context
- make sign-in and sign-up set cookies via `nextCookies()`
- add a role or organization check with `authorize`
- drop hand-written auth middleware for the adapter

## Example prompts
- Add Better Auth to my safe actions
- Set up `betterAuth(auth)` with a typed context
- Add an admin role check in `authorize` for this action

## Similar
- [better-auth](https://affpapa.org/en/nevibecode/skills/better-auth.md) — skill · 3600 installs
- [better-auth-authentication](https://affpapa.org/en/nevibecode/skills/better-auth-authentication.md) — skill · 389 installs
- [better-auth-core](https://affpapa.org/en/nevibecode/skills/better-auth-core.md) — skill · 358 installs
- [nuxt-better-auth](https://affpapa.org/en/nevibecode/skills/nuxt-better-auth.md) — skill · 2000 installs
- [better-auth-security-best-practices](https://affpapa.org/en/nevibecode/skills/better-auth-security-best-practices.md) — skill · 48000 installs
- [create-auth](https://affpapa.org/en/nevibecode/skills/create-auth.md) — skill · 13400 installs

From the same set: [next-safe-action/skills](https://affpapa.org/en/nevibecode/by/next-safe-action/skills.md) — 2 cards

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
