safe-action-better-auth

Adds Better Auth authentication middleware to next-safe-action server action clients.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires @next-safe-action/adapter-better-auth, better-auth, next-safe-action, zod.

What problem does it solve? Wiring Better Auth session checks into next-safe-action server actions by hand means re-fetching sessions, losing type inference, and handling unauthorized responses manually. This Skill guides the setup of the @next-safe-action/adapter-better-auth middleware so authenticated action clients get typed session context automatically. ## Core Features & Use Cases - Authenticated action clients: Create an authClient via betterAuth(auth) that fetches the session, rejects unauthenticated requests with unauthorized(), and injects typed ctx.auth.user and ctx.auth.session. - Custom authorization: Use the authorize callback for role-based access, redirects to login, organization-scoped checks, and optional authentication without re-fetching the session. - Correct Next.js integration: Covers the required nextCookies() plugin ordering and the experimental authInterrupts flag, plus common anti-patterns to avoid. - Use Case: You are building a Next.js app with Better Auth and want an admin-only server action; the Skill shows how to compose an adminClient that rejects non-admin users before the action handler runs. ## Quick Start Set up an authenticated next-safe-action client using the Better Auth adapter so my server actions receive a typed session in ctx.auth.

Frequently Asked Questions about safe-action-better-auth

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

FAQPage Schema
How do I add Better Auth to next-safe-action server actions?▼

Install @next-safe-action/adapter-better-auth and call actionClient.use(betterAuth(auth)) to create an authenticated client. The middleware fetches the session from request headers and injects typed ctx.auth.user and ctx.auth.session into your action handlers.

How do I implement role-based access control in Next.js server actions?▼

Pass an authorize callback to betterAuth(auth, { authorize }) and check authData.user.role, calling unauthorized() when the check fails. The session is pre-fetched as authData, so you never need to re-fetch it inside the callback.

Why is my Better Auth session null inside server actions?▼

The session is null when the nextCookies() plugin is missing from your Better Auth config, since actions that set cookies require it. Add nextCookies() from better-auth/next-js as the last plugin in the plugins array.

Does unauthorized() from next/navigation require special Next.js config?▼

Yes, unauthorized() requires the experimental authInterrupts flag set to true in next.config.ts, otherwise it throws a runtime error. If you use redirect() in the authorize callback instead, the flag is not needed.

Can I allow anonymous access in some server actions with Better Auth?▼

Yes, write an authorize callback that always calls next({ ctx: { auth: authData } }) without calling unauthorized(). The action then receives ctx.auth as null for anonymous users and a typed session for logged-in users.