safe-action-middleware

Implements authentication, logging, and validation middleware for next-safe-action server actions.

Updated Jul 8, 2026
One-click install
npx skills add https://github.com/NarixHine/uni-chem --skill safe-action-middleware-narixhine
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: safe-action-middleware
Source: https://github.com/NarixHine/uni-chem/tree/main/.agents/skills/safe-action-middleware
Command: npx skills add https://github.com/NarixHine/uni-chem --skill safe-action-middleware-narixhine

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires next-safe-action, zod.

What problem does it solve? Building secure Next.js server actions requires repetitive boilerplate for authentication, authorization, logging, and rate limiting. This Skill provides patterns for composing reusable middleware chains with next-safe-action so these cross-cutting concerns are handled consistently and type-safely. ## Core Features & Use Cases - Pre- and Post-Validation Middleware: Use .use() for session checks and rate limiting before validation, and .useValidated() for authorization logic that depends on typed, parsed input. - Auth & Authorization Patterns: Ready-made examples for session lookup, role-based access, resource ownership checks, and API key authentication. - Observability & Reusability: Structured logging, request timing, Sentry error reporting, and standalone middleware via createMiddleware() and createValidatedMiddleware() with type constraints. - Use Case: You need an admin-only action that updates a post. Chain an auth middleware that injects userId into context, then a useValidated() middleware that verifies the user owns the post before the action runs. ## Quick Start Ask the AI to create a next-safe-action middleware chain that requires a logged-in session and checks resource ownership before running a server action.

Frequently Asked Questions about safe-action-middleware

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

FAQPage Schema
How do I add authentication middleware to next-safe-action?▼

Use .use() on the action client to check the session before the action runs. Throw an error if no session exists, and pass the user ID to downstream handlers via next({ ctx: { userId } }).

What is the difference between use() and useValidated() in next-safe-action?▼

use() runs before input validation and receives raw clientInput, suiting auth and logging. useValidated() runs after validation with typed parsedInput, suiting authorization checks like resource ownership.

How do I create reusable middleware with next-safe-action?▼

Use createMiddleware().define() for pre-validation middleware or createValidatedMiddleware().define() for post-validation middleware. Both support generic type constraints so the middleware only composes with clients providing required context.

Why does my next-safe-action middleware hang without running the action?▼

The middleware is missing a return on the next() call. Always write return next({ ctx }) so the result propagates back up the chain; calling next() without returning leaves the action unresolved.

Can I use useValidated() without defining an input schema?▼

No. You must call .inputSchema() or .bindArgsSchemas() before .useValidated(), otherwise the code will not compile. Schema methods also cannot be called after .useValidated().

How do I handle errors in next-safe-action middleware without breaking redirects?▼

Re-throw errors that carry a digest property, since Next.js uses those for redirect and notFound control flow. Catch and convert only genuine application errors into serverError results.