workos-authkit-nextjs
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWorkOS AuthKit for Next.js
适用于Next.js的WorkOS AuthKit
Step 1: Fetch SDK Documentation (BLOCKING)
步骤1:获取SDK文档(阻塞操作)
STOP. Do not proceed until complete.
WebFetch:
https://github.com/workos/authkit-nextjs/blob/main/README.mdThe README is the source of truth. If this skill conflicts with README, follow README.
停止操作。完成此步骤前请勿继续。
WebFetch:
https://github.com/workos/authkit-nextjs/blob/main/README.mdREADME是权威来源。如果本技能内容与README冲突,请以README为准。
Step 2: Pre-Flight Validation
步骤2:前期验证检查
Project Structure
项目结构
- Confirm or
next.config.jsexistsnext.config.mjs - Confirm contains
package.jsondependency"next"
- 确认项目中存在或
next.config.jsnext.config.mjs - 确认中包含
package.json依赖"next"
Environment Variables
环境变量
Check for:
.env.local- - starts with
WORKOS_API_KEYsk_ - - starts with
WORKOS_CLIENT_IDclient_ - - valid callback URL
NEXT_PUBLIC_WORKOS_REDIRECT_URI - - 32+ characters
WORKOS_COOKIE_PASSWORD
检查文件中是否存在以下变量:
.env.local- - 以
WORKOS_API_KEY开头sk_ - - 以
WORKOS_CLIENT_ID开头client_ - - 有效的回调URL
NEXT_PUBLIC_WORKOS_REDIRECT_URI - - 长度不少于32个字符
WORKOS_COOKIE_PASSWORD
Step 3: Install SDK
步骤3:安装SDK
Detect package manager, install SDK package from README.
Verify: SDK package exists in node_modules before continuing.
检测包管理器,按照README中的说明安装SDK包。
验证: 继续操作前,请确认SDK包已存在于node_modules目录中。
Step 4: Version Detection (Decision Tree)
步骤4:版本检测(决策树)
Read Next.js version from :
package.jsonNext.js version?
|
+-- 16+ --> Create proxy.ts at project root
|
+-- 15 --> Create middleware.ts (cookies() is async - handlers must await)
|
+-- 13-14 --> Create middleware.ts (cookies() is sync)Critical: File MUST be at project root (or if using src directory). Never in .
src/app/Next.js 15+ async note: All route handlers and middleware accessing cookies must be async and properly await cookie operations. This is a breaking change from Next.js 14.
Middleware/proxy code: See README for export pattern.
authkitMiddleware()从中读取Next.js版本:
package.jsonNext.js version?
|
+-- 16+ --> Create proxy.ts at project root
|
+-- 15 --> Create middleware.ts (cookies() is async - handlers must await)
|
+-- 13-14 --> Create middleware.ts (cookies() is sync)关键注意事项: 文件必须位于项目根目录(如果使用src目录,则放在下)。绝对不能放在目录中。
src/app/Next.js 15+异步注意事项: 所有访问cookies的路由处理器和中间件必须是异步的,并且要正确等待cookie操作完成。这是Next.js 14到15的破坏性变更。
中间件/代理代码:请查看README中的导出模式。
authkitMiddleware()Existing Middleware (IMPORTANT)
已有中间件(重要)
If already exists with custom logic (rate limiting, logging, headers, etc.), use the composable function instead of .
middleware.tsauthkit()authkitMiddlewarePattern for composing with existing middleware:
typescript
import { NextRequest, NextResponse } from 'next/server';
import { authkit, handleAuthkitHeaders } from '@workos-inc/authkit-nextjs';
export default async function middleware(request: NextRequest) {
// 1. Get auth session and headers from AuthKit
const { session, headers, authorizationUrl } = await authkit(request);
const { pathname } = request.nextUrl;
// 2. === YOUR EXISTING MIDDLEWARE LOGIC ===
// Rate limiting, logging, custom headers, etc.
const rateLimitResult = checkRateLimit(request);
if (!rateLimitResult.allowed) {
return new NextResponse('Too Many Requests', { status: 429 });
}
// 3. Protect routes - redirect to auth if needed
if (pathname.startsWith('/dashboard') && !session.user && authorizationUrl) {
return handleAuthkitHeaders(request, headers, { redirect: authorizationUrl });
}
// 4. Continue with AuthKit headers properly handled
return handleAuthkitHeaders(request, headers);
}Key functions:
- - Returns
authkit(request)for composition{ session, headers, authorizationUrl } - - Ensures AuthKit headers pass through correctly
handleAuthkitHeaders(request, headers, options?) - For rewrites, use and
partitionAuthkitHeaders()(see README)applyResponseHeaders()
Critical: Always return via to ensure works in pages.
handleAuthkitHeaders()withAuth()如果已存在且包含自定义逻辑(限流、日志、请求头处理等),请使用**组合函数**替代。
middleware.tsauthkit()authkitMiddleware与现有中间件组合的模式:
typescript
import { NextRequest, NextResponse } from 'next/server';
import { authkit, handleAuthkitHeaders } from '@workos-inc/authkit-nextjs';
export default async function middleware(request: NextRequest) {
// 1. 从AuthKit获取认证会话和请求头
const { session, headers, authorizationUrl } = await authkit(request);
const { pathname } = request.nextUrl;
// 2. === 您的现有中间件逻辑 ===
// 限流、日志、自定义请求头等
const rateLimitResult = checkRateLimit(request);
if (!rateLimitResult.allowed) {
return new NextResponse('Too Many Requests', { status: 429 });
}
// 3. 保护路由 - 如需认证则重定向
if (pathname.startsWith('/dashboard') && !session.user && authorizationUrl) {
return handleAuthkitHeaders(request, headers, { redirect: authorizationUrl });
}
// 4. 正确处理AuthKit请求头后继续
return handleAuthkitHeaders(request, headers);
}核心函数:
- - 返回
authkit(request)用于组合逻辑{ session, headers, authorizationUrl } - - 确保AuthKit请求头正确传递
handleAuthkitHeaders(request, headers, options?) - 如需重写路由,请使用和
partitionAuthkitHeaders()(详见README)applyResponseHeaders()
关键注意事项: 必须通过返回结果,以确保页面中的正常工作。
handleAuthkitHeaders()withAuth()Step 5: Create Callback Route
步骤5:创建回调路由
Parse to determine route path:
NEXT_PUBLIC_WORKOS_REDIRECT_URIURI path --> Route location
/auth/callback --> app/auth/callback/route.ts
/callback --> app/callback/route.tsUse from SDK. Do not write custom OAuth logic.
handleAuth()CRITICAL for Next.js 15+: The route handler MUST be async and properly await handleAuth():
typescript
// CORRECT - Next.js 15+ requires async route handlers
export const GET = handleAuth();
// If handleAuth returns a function, ensure it's awaited in request contextCheck README for exact usage. If build fails with "cookies outside request scope", the handler is likely missing async/await.
解析以确定路由路径:
NEXT_PUBLIC_WORKOS_REDIRECT_URIURI路径 --> 路由位置
/auth/callback --> app/auth/callback/route.ts
/callback --> app/callback/route.ts使用SDK中的。请勿编写自定义OAuth逻辑。
handleAuth()Next.js 15+关键要求: 路由处理器必须是异步的,并且要正确等待handleAuth()执行完成:
typescript
// 正确写法 - Next.js 15+要求异步路由处理器
export const GET = handleAuth();
// 如果handleAuth返回一个函数,请确保在请求上下文中等待其执行请查看README获取确切用法。如果构建失败并提示"cookies outside request scope",则说明处理器可能缺少async/await。
Step 6: Provider Setup (REQUIRED)
步骤6:Provider配置(必填)
CRITICAL: You MUST wrap the app in in .
AuthKitProviderapp/layout.tsxThis is required for:
- Client-side auth state via hook
useAuth() - Consistent auth UX across client/server boundaries
- Proper migration from Auth0 (which uses client-side auth)
tsx
// app/layout.tsx
import { AuthKitProvider } from '@workos-inc/authkit-nextjs';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<AuthKitProvider>{children}</AuthKitProvider>
</body>
</html>
);
}Check README for exact import path - it may be a subpath export like .
@workos-inc/authkit-nextjs/componentsDo NOT skip this step even if using server-side auth patterns elsewhere.
关键注意事项: 您必须在中使用包裹整个应用。
app/layout.tsxAuthKitProvider这是以下功能的必要条件:
- 通过钩子在客户端获取认证状态
useAuth() - 在客户端/服务端边界保持一致的认证用户体验
- 从Auth0平滑迁移(Auth0使用客户端认证)
tsx
// app/layout.tsx
import { AuthKitProvider } from '@workos-inc/authkit-nextjs';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<AuthKitProvider>{children}</AuthKitProvider>
</body>
</html>
);
}请查看README确认确切的导入路径 - 它可能是子路径导出,例如。
@workos-inc/authkit-nextjs/components请勿跳过此步骤,即使您在其他地方使用了服务端认证模式。
Step 7: UI Integration
步骤7:UI集成
Add auth UI to using SDK functions. See README for , , usage.
app/page.tsxgetUsergetSignInUrlsignOut使用SDK函数在中添加认证UI。请查看README中、、的用法。
app/page.tsxgetUsergetSignInUrlsignOutVerification Checklist (ALL MUST PASS)
验证清单(必须全部通过)
Run these commands to confirm integration. Do not mark complete until all pass:
bash
undefined运行以下命令确认集成是否成功。必须全部通过后才能标记完成:
bash
undefined1. Check middleware/proxy exists (one should match)
1. 检查中间件/代理文件是否存在(以下其中一个应存在)
ls proxy.ts middleware.ts src/proxy.ts src/middleware.ts 2>/dev/null
ls proxy.ts middleware.ts src/proxy.ts src/middleware.ts 2>/dev/null
2. CRITICAL: Check AuthKitProvider is in layout (REQUIRED)
2. 关键检查:确认AuthKitProvider已添加到布局中(必填)
grep "AuthKitProvider" app/layout.tsx || echo "FAIL: AuthKitProvider missing from layout"
grep "AuthKitProvider" app/layout.tsx || echo "失败:布局中缺少AuthKitProvider"
3. Check callback route exists
3. 检查回调路由是否存在
find app -name "route.ts" -path "/callback/"
find app -name "route.ts" -path "/callback/"
4. Build succeeds
4. 构建是否成功
npm run build
**If check #2 fails:** Go back to Step 6 and add AuthKitProvider. This is not optional.npm run build
**如果检查2失败:** 返回步骤6添加AuthKitProvider。此步骤为必填项,不可省略。Error Recovery
错误排查
"cookies was called outside a request scope" (Next.js 15+)
"cookies was called outside a request scope"(Next.js 15+)
Most common cause: Route handler not properly async or missing await.
Fix for callback route:
- Check that is exported directly:
handleAuth()export const GET = handleAuth(); - If using custom wrapper, ensure it's and awaits any cookie operations
async - Verify authkit-nextjs SDK version supports Next.js 15+ (check README for compatibility)
- Never call at module level - only inside request handlers
cookies()
This error causes OAuth codes to expire ("invalid_grant"), so fix the handler first.
最常见原因: 路由处理器未正确设置为异步,或缺少await。
回调路由的修复方法:
- 确认是直接导出的:
handleAuth()export const GET = handleAuth(); - 如果使用自定义包装函数,请确保它是的,并且等待所有cookie操作完成
async - 验证authkit-nextjs SDK版本是否支持Next.js 15+(查看README确认兼容性)
- 绝对不要在模块级别调用- 只能在请求处理器内部调用
cookies()
此错误会导致OAuth代码过期("invalid_grant"),因此请优先修复处理器。
"middleware.ts not found"
"middleware.ts not found"
- Check: File at project root or , not inside
src/app/ - Check: Filename matches Next.js version (proxy.ts for 16+, middleware.ts for 13-15)
- 检查:文件是否位于项目根目录或下,而不是
src/目录中app/ - 检查:文件名是否与Next.js版本匹配(16+使用proxy.ts,13-15使用middleware.ts)
"Cannot use getUser in client component"
"Cannot use getUser in client component"
- Check: Component has no directive, or
'use client' - Check: Move auth logic to server component/API route
- 检查:组件是否没有指令,或者
'use client' - 检查:将认证逻辑迁移到服务端组件/API路由
"Module not found" for SDK import
"Module not found" for SDK import
- Check: SDK installed before writing imports
- Check: SDK package directory exists in node_modules
- 检查:编写导入语句前是否已安装SDK
- 检查:node_modules中是否存在SDK包目录
"withAuth route not covered by middleware"
"withAuth route not covered by middleware"
- Check: Middleware/proxy file exists at correct location
- Check: Matcher config includes the route path
- 检查:中间件/代理文件是否位于正确位置
- 检查:匹配器配置是否包含该路由路径
Build fails after AuthKitProvider
添加AuthKitProvider后构建失败
- Check: README for correct import path (may be subpath export)
- Check: No client/server boundary violations
- 检查:README中的正确导入路径(可能是子路径导出)
- 检查:是否存在客户端/服务端边界冲突
NEXTPUBLIC prefix issues
NEXT_PUBLIC前缀问题
- Client components need prefix
NEXT_PUBLIC_* - Server components use plain env var names
- 客户端组件需要前缀
NEXT_PUBLIC_* - 服务端组件使用普通的环境变量名称