workos-authkit-react-router
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWorkOS AuthKit for React Router
适用于React Router的WorkOS AuthKit
Decision Tree
决策流程
1. Fetch README (BLOCKING)
2. Detect router mode
3. Follow README for that mode
4. Verify with checklist below1. 获取README(阻塞操作)
2. 检测路由模式
3. 遵循对应模式的README指引
4. 使用下方的检查清单进行验证Phase 1: Fetch SDK Documentation (BLOCKING)
阶段1:获取SDK文档(阻塞操作)
STOP - Do not write any code until this completes.
WebFetch:
https://github.com/workos/authkit-react-router/blob/main/README.mdThe README is the source of truth. If this skill conflicts with README, follow the README.
暂停 - 完成此步骤前请勿编写任何代码。
WebFetch:
https://github.com/workos/authkit-react-router/blob/main/README.mdREADME是权威来源。如果本技能内容与README冲突,请以README为准。
Phase 2: Detect Router Mode
阶段2:检测路由模式
| Mode | Detection Signal | Key Indicator |
|---|---|---|
| v7 Framework | | Routes in |
| v7 Data | | Loaders in route config |
| v7 Declarative | | Routes as JSX, no loaders |
| v6 | package.json version | Similar to v7 Declarative |
Detection order:
- Check for (Framework mode)
react-router.config.ts - Grep for (Data mode)
createBrowserRouter - Check package.json version (v6 vs v7)
- Default to Declarative if v7 with
<BrowserRouter>
| 模式 | 检测信号 | 关键指标 |
|---|---|---|
| v7 Framework | 存在 | 路由位于 |
| v7 Data | 源码中存在 | 路由配置中包含Loaders |
| v7 Declarative | 使用 | 路由以JSX形式编写,无loaders |
| v6 | package.json中版本为 | 与v7 Declarative模式类似 |
检测顺序:
- 检查是否存在(Framework模式)
react-router.config.ts - 搜索(Data模式)
createBrowserRouter - 查看package.json中的版本(v6 vs v7)
- 若为v7且使用,默认归为Declarative模式
<BrowserRouter>
Phase 3: Follow README
阶段3:遵循README指引
Based on detected mode, apply the corresponding README section. The README contains current API signatures and code patterns.
根据检测到的模式,应用README中对应的章节内容。README包含最新的API签名和代码模式。
Critical Distinctions
关键区别
authLoader vs authkitLoader
authLoader vs authkitLoader
| Function | Purpose | Where to use |
|---|---|---|
| OAuth callback handler | Callback route ONLY |
| Fetch user data in routes | Any route needing auth |
Common mistake: Using for callback route. Use .
authkitLoaderauthLoader()| 函数 | 用途 | 使用场景 |
|---|---|---|
| OAuth回调处理程序 | 仅用于回调路由 |
| 在路由中获取用户数据 | 任何需要身份验证的路由 |
常见错误: 在回调路由中使用。请使用。
authkitLoaderauthLoader()Root Route Requirement
根路由要求
Auth loader MUST be on root route for child routes to access auth context.
Wrong: Auth loader only on
Right: Auth loader on (root), children inherit context
/dashboard/身份验证loader必须配置在根路由上,以便子路由能够访问身份验证上下文。
错误做法: 仅在路由上配置身份验证loader
正确做法: 在(根路由)上配置身份验证loader,子路由继承上下文
/dashboard/Environment Variables
环境变量
Required in or :
.env.env.local- - starts with
WORKOS_API_KEYsk_ - - starts with
WORKOS_CLIENT_IDclient_ - - full URL (e.g.,
WORKOS_REDIRECT_URI)http://localhost:3000/auth/callback - - 32+ chars (server modes only)
WORKOS_COOKIE_PASSWORD
需在或中配置:
.env.env.local- - 以
WORKOS_API_KEY开头sk_ - - 以
WORKOS_CLIENT_ID开头client_ - - 完整URL(例如:
WORKOS_REDIRECT_URI)http://localhost:3000/auth/callback - - 32个字符以上(仅服务器模式需要)
WORKOS_COOKIE_PASSWORD
Verification Checklist
验证检查清单
After implementation, verify:
- SDK installed in node_modules (package name from README)
- Callback route path matches path segment
WORKOS_REDIRECT_URI - Auth loader/provider on root route (not just child routes)
- Build succeeds: exits 0
npm run build - Correct mode pattern applied (loaders vs hooks)
完成集成后,请验证以下内容:
- SDK已安装在node_modules中(包名以README为准)
- 回调路由路径与的路径段匹配
WORKOS_REDIRECT_URI - 身份验证loader/提供者配置在根路由上(而非仅子路由)
- 构建成功:执行后返回0
npm run build - 应用了正确的模式(loaders vs 钩子)
Error Recovery
错误排查
"loader is not a function"
"loader is not a function"
Cause: Using loader pattern in Declarative/v6 mode
Fix: Declarative/v6 modes use + hook, not loaders
AuthKitProvideruseAuth原因: 在Declarative/v6模式中使用了loader模式
解决方法: Declarative/v6模式使用 + 钩子,而非loaders
AuthKitProvideruseAuthAuth state not available in child routes
子路由无法获取身份验证状态
Cause: Auth loader missing from root route
Fix: Add (or ) to root route so children inherit context
authkitLoaderAuthKitProvider原因: 根路由缺少身份验证loader
解决方法: 为根路由添加(或),使子路由能够继承上下文
authkitLoaderAuthKitProvideruseAuth returns undefined
useAuth返回undefined
Cause: Missing wrapper
Fix: Wrap app with (required for Declarative/v6 modes)
AuthKitProviderAuthKitProvider原因: 缺少包裹
解决方法: 使用包裹应用(Declarative/v6模式必需)
AuthKitProviderAuthKitProviderCallback route 404
回调路由返回404
Cause: Route path mismatch with
Fix: Extract exact path from env var, create route at that path
WORKOS_REDIRECT_URI原因: 路由路径与不匹配
解决方法: 从环境变量中提取精确路径,创建对应路由
WORKOS_REDIRECT_URI"Module not found" for SDK
SDK提示“Module not found”
Cause: SDK not installed
Fix: Install SDK, wait for completion, verify before writing imports
node_modules原因: 未安装SDK
解决方法: 安装SDK,等待安装完成,编写导入语句前确认中存在该SDK
node_modules