server-auth-actions — Claude Code rule: what it does, install
The server-auth-actions rule puts authentication and permission checks inside every Server Action. Use it to guard server mutations that callers can invoke directly.
MIT ★ 4,719 updated 2026-10-24
.claude/rules/server-auth-actions.mdWhat server-auth-actions does
The rule treats functions marked with "use server" as public entry points, much like API routes. Each Server Action should validate its input, check the session, confirm the caller’s permissions, and only then change stored data. The examples use verifySession, unauthorized, and zod schemas.
A middleware, layout guard, or page check does not protect the action by itself because the function may be called directly. Deleting a user therefore needs a check for an administrator or that same user, while profile updates must belong to the signed-in account. In Claude Code, install the rule at .claude/rules/server-auth-actions.md; Codex is also listed as a supported platform.
What it helps with
- secure a Server Action against unauthorised calls
- stop users from deleting other accounts
- allow profile edits only for the owner
- validate mutation input before writing data
Example prompts for the agent
- Review every Server Action in this project and put the session check inside each function.
- Rewrite user deletion so an administrator or the account owner can perform it.
- Add
zodvalidation before the profile update and reject edits to another user’s profile.
How to install server-auth-actions
Manually: put the files in place
.claude/rules/server-auth-actions.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/server-auth-actions.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/server-auth-actions.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/server-auth-actions.mdManually: put the files in place
.clinerules/server-auth-actions.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about server-auth-actions
How do I install server-auth-actions in Claude Code?
Add the rule file at .claude/rules/server-auth-actions.md.
What does server-auth-actions do?
It requires each Server Action to validate input, authenticate the caller, and check permissions before performing a data mutation.
Why must authentication happen inside the Server Action?
A Server Action can be invoked directly, so middleware, layout guards, and page-level checks do not secure the function on their own.
Source text
server-auth-actions.md · MIT
---
title: Authenticate Server Actions Like API Routes
impact: CRITICAL
impactDescription: prevents unauthorized access to server mutations
tags: server, server-actions, authentication, security, authorization
---
## Authenticate Server Actions Like API Routes
**Impact: CRITICAL (prevents unauthorized access to server mutations)**
Server Actions (functions with `"use server"`) are exposed as public endpoints, just like API routes. Always verify authentication and authorization **inside** each Server Action—do not rely solely on middleware, layout guards, or page-level checks, as Server Actions can be invoked directly.
Next.js documentation explicitly states: "Treat Server Actions with the same security considerations as public-facing API endpoints, and verify if the user is allowed to perform a mutation."
**Incorrect (no authentication check):**
```typescript
"use server"
export async function deleteUser(userId: string) {
// Anyone can call this! No auth check
await db.user.delete({ where: { id: userId } })
return { success: true }
}
```
**Correct (authentication inside the action):**
```typescript
"use server"
import { verifySession } from "@/lib/auth"
import { unauthorized } from "@/lib/errors"
export async function deleteUser(userId: string) {
// Always check auth inside the action
const session = await verifySession()
if (!session) {
throw unauthorized("Must be logged in")
}
// Check authorization too
if (session.user.role !== "admin" && session.user.id !== userId) {
throw unauthorized("Cannot delete other users")
}
await db.user.delete({ where: { id: userId } })
return { success: true }
}
```
**With input validation:**
```typescript
"use server"
import { verifySession } from "@/lib/auth"
import { z } from "zod"
const updateProfileSchema = z.object({
userId: z.string().uuid(),
name: z.string().min(1).max(100),
email: z.string().email(),
})
export async function updateProfile(data: unknown) {
// Validate input first
const validated = updateProfileSchema.parse(data)
// Then authenticate
const session = await verifySession()
if (!session) {
throw new Error("Unauthorized")
}
// Then authorize
if (session.user.id !== validated.userId) {
throw new Error("Can only update own profile")
}
// Finally perform the mutation
await db.user.update({
where: { id: validated.userId },
data: {
name: validated.name,
email: validated.email,
},
})
return { success: true }
}
```
Reference: [https://nextjs.org/docs/app/guides/authentication](https://nextjs.org/docs/app/guides/authentication)
© zebbern · MIT · zebbern/claude-code-guide