@ackplus/nest-auth-react

中文文档
2.18.0发布于 2026/11/03MITTypeScriptESM + CJS近 30 天下载 2244

React SDK for NestJS authentication - hooks, guards, and Next.js integration

原始使用文档
文档目录11 节
  1. Why use it
  2. Install
  3. AI coding agents
  4. What's included
  5. Minimal example — SPA
  6. Minimal example — Next.js App Router
  7. Role & permission gating
  8. Documentation
  9. Companion packages
  10. Links
  11. License

@ackplus/nest-auth-react

npm version npm downloads license

React provider, hooks, guards, and first-class Next.js App Router helpers for @ackplus/nest-auth. Headless — bring your own UI.

📚 Full documentation: ack-solutions.github.io/nest-auth/docs/react


Why use it

A thin React layer on top of @ackplus/nest-auth-client that gives you everything you'd build by hand: a context provider, narrow re-rendering hooks, guard components, route-level guards via Next.js Server Components, and cross-tab sync.

It's headless — no styled buttons, no opinionated forms — so it drops into any React 18 / 19 app or Next.js App Router project without fighting your design system.

Install

pnpm add @ackplus/nest-auth-react @ackplus/nest-auth-client
PeerVersions
react^18.0.0 || ^19.0.0

AI coding agents

Using Claude Code, Codex, Cursor or another AI coding agent? Install the Nest Auth skill so it knows this SDK's real API:

npx @ackplus/nest-auth-skill

It detects your stack (or pass --stack react) and only adds agent instruction files; your app is not changed.

What's included

SurfaceAPI
Provider<AuthProvider> (client-rendered) · <NextAuthProvider> (with SSR initial-state hydration)
HooksuseNestAuth · useUser · useSession · useAccessToken · useAuthStatus · useHasRole · useHasPermission
Guard components<AuthGuard> · <GuestGuard> · <RequireRole> · <RequirePermission>
HOCswithRequireRole · withRequirePermission (+ createRequireRoleHOC / createRequirePermissionHOC factories)
Next.js helperscreateNextAuthHelpers returning { getServerAuth, withAuth, createInitialState }
Cross-tab syncCrossTabSync / createCrossTabSync() — BroadcastChannel with localStorage fallback

Minimal example — SPA

import { AuthClient } from '@ackplus/nest-auth-client';
import { AuthProvider, useNestAuth, AuthGuard, useUser } from '@ackplus/nest-auth-react';

const client = new AuthClient({ baseUrl: 'https://api.example.com' });

export default function App() {
  return (
    <AuthProvider client={client}>
      <Routes />
    </AuthProvider>
  );
}

function Dashboard() {
  return (
    <AuthGuard fallback={<a href="/login">Sign in</a>}>
      <DashboardInner />
    </AuthGuard>
  );
}

function DashboardInner() {
  const user = useUser();
  return <h1>Hello {user?.email}</h1>;
}

function LoginForm() {
  const { login, error, isLoading } = useNestAuth();
  return (
    <form onSubmit={async (e) => {
      e.preventDefault();
      await login({ credentials: { email, password } });
    }}>
      {/* … */}
      <button disabled={isLoading}>Sign in</button>
      {error && <p>{error.message}</p>}
    </form>
  );
}

Minimal example — Next.js App Router

// app/layout.tsx (Server Component)
import { cookies } from 'next/headers';
import { NextAuthProvider } from '@ackplus/nest-auth-react';
import { client, authHelpers } from '@/lib/auth';

export default async function RootLayout({ children }) {
  const initialState = await authHelpers.getServerAuth(await cookies());

  return (
    <html lang="en">
      <body>
        <NextAuthProvider client={client} initialState={initialState}>
          {children}
        </NextAuthProvider>
      </body>
    </html>
  );
}
// app/api/private/route.ts
import { authHelpers } from '@/lib/auth';

export const GET = authHelpers.withAuth(async (req) => {
  return Response.json({ user: req.auth.user });
});

Full Next.js guide →

Role & permission gating

import { RequireRole, RequirePermission, useHasRole } from '@ackplus/nest-auth-react';

<RequireRole role="admin" fallback={<NotAuthorized />}>
  <AdminPanel />
</RequireRole>

<RequirePermission permission={['orders.read', 'orders.write']} matchAll>
  <OrderEditor />
</RequirePermission>

// Or via hook
const isAdmin = useHasRole('admin');

Documentation

SectionWhat's there
Provider<AuthProvider>, <NextAuthProvider>, all props
HooksEvery hook with return shape and re-render notes
GuardsComponents + HOCs + factories
Next.jsApp Router SSR pattern, withAuth, middleware redirect
Cross-Tab SyncMulti-tab logout/login propagation
Quickstart — ReactSPA setup end to end
Quickstart — Next.jsApp Router setup end to end

Companion packages

PackageUse when
@ackplus/nest-authThe NestJS backend module this client talks to
@ackplus/nest-auth-clientThe vanilla JS client this package wraps
@ackplus/nest-auth-contractsShared TS types (already a transitive dep)

All four packages release together with the same version number. Pin them all to the same version.

License

MIT

版本与发布记录

@ackplus/nest-auth-react 共发布 118 个版本,首次发布于 2025/11/22;过去 12 个月发布了 118 个版本。

版本发布日期距今
2.18.0latest2026/11/034 天前
2.16.02026/10/2711 天前
2.15.02026/10/2612 天前
2.14.12026/10/2414 天前
2.14.02026/10/1226 天前
2.13.12026/10/1028 天前

其他发布通道:beta → 2.0.0-beta.26。

关于 @ackplus/nest-auth-react 的常见问题

@ackplus/nest-auth-react 是什么?

React SDK for NestJS authentication - hooks, guards, and Next.js integration

如何安装 @ackplus/nest-auth-react?

在项目目录运行 npm install @ackplus/nest-auth-react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.0.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ackplus/nest-auth-react 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ackplus/nest-auth-react 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ackplus/nest-auth-react 可以商用吗?

@ackplus/nest-auth-react 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ackplus/nest-auth-react 的下载量有多少?

根据 npm 官方下载统计,@ackplus/nest-auth-react 在 2026/10/05 – 2026/11/03 这 30 天内被下载 2,244 次,日均约 75 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ackplus/nest-auth-react 还在维护吗?

最新版本 2.18.0 发布于 2026/11/03(4 天前);至今共发布 118 个版本,其中过去 12 个月发布了 118 个;首次发布于 2025/11/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。