Skill站外收录 @skilllib/skill-2014279397426016260
clerk-local-dev-loop
支持本地开发环境下的 Clerk 认证配置,实现开发实例与测试用户的快速搭建,提供热重载支持及认证流程的单元测试模拟,确保开发与生产环境的安全隔离。
效率工具 AI 技能
Skills.MD
# Clerk Local Dev Loop
## Overview
Configure an efficient local development workflow with Clerk authentication.
## Prerequisites
- Clerk SDK installed
- Development and production instances in Clerk dashboard
- Node.js development environment
## Instructions
### Step 1: Configure Development Instance
```bash
# Use development keys in .env.local
cat > .env.local << 'EOF'
# Development keys (start with pk_test_ and sk_test_)
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_...
CLERK_SECRET_KEY=sk_test_...
# Optional: Custom sign-in/sign-up URLs
NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up
NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL=/dashboard
NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/onboarding
EOF
```
### Step 2: Set Up Test Users
```typescript
// scripts/create-test-user.ts
// Use Clerk Backend SDK for test user management
import { clerkClient } from '@clerk/nextjs/server'
async function createTestUser() {
const user = await clerkClient.users.createUser({
emailAddress: ['test@example.com'],
password: 'testpassword123',
firstName: 'Test',
lastName: 'User'
})
console.log('Created test user:', user.id)
}
```
### Step 3: Configure Hot Reload
```typescript
// next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
// Clerk works with fast refresh out of the box
reactStrictMode: true,
// Environment-specific configuration
env: {
CLERK_DOMAIN: process.env.NODE_ENV === 'development'
? 'clerk.your-dev-domain.com'
: 'clerk.your-prod-domain.com'
}
}
module.exports = nextConfig
```
### Step 4: Development Scripts
```json
{
"scripts": {
"dev": "next dev",
"dev:https": "next dev --experimental-https",
"clerk:dev": "npx @clerk/cli dev",
"test:auth": "node scripts/test-auth.js"
}
}
```
### Step 5: Mock Authentication for Tests
```typescript
// __tests__/setup.ts
import { vi } from 'vitest'
// Mock Clerk for unit tests
vi.mock('@clerk/nextjs', () => ({
auth: () => ({ userId: 'test-user-id' }),
currentUser: () => ({
id: 'test-user-id',
firstName: 'Test',
emailAddresses: [{ emailAddress: 'test@example.com' }]
}),
useUser: () => ({
user: { id: 'test-user-id', firstName: 'Test' },
isLoaded: true,
isSignedIn: true
})
}))
```
## Output
- Development environment configured
- Test users available
- Hot reload working with auth
- Mocked auth for testing
## Error Handling
| Error | Cause | Solution |
|-------|-------|----------|
| Development/Production mismatch | Using prod keys in dev | Use pk_test_/sk_test_ keys locally |
| SSL Required | Clerk needs HTTPS | Use `next dev --experimental-https` |
| Cookies Not Set | Wrong domain config | Check Clerk dashboard domain settings |
| Session Not Persisting | LocalStorage issues | Clear browser storage, check domain |
## Examples
### Environment Switching
```typescript
// lib/clerk.ts
export const clerkConfig = {
publishableKey: process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY!,
signInUrl: process.env.NEXT_PUBLIC_CLERK_SIGN_IN_URL || '/sign-in',
signUpUrl: process.env.NEXT_PUBLIC_CLERK_SIGN_UP_URL || '/sign-up',
}
// Validate configuration
if (!clerkConfig.publishableKey.startsWith('pk_')) {
throw new Error('Invalid Clerk publishable key')
}
```
### Local Webhook Testing
```bash
# Use ngrok or similar for webhook testing
npx ngrok http 3000
# Update webhook URL in Clerk dashboard to ngrok URL
# https://abc123.ngrok.io/api/webhooks/clerk
```
## Resources
- [Clerk Development Mode](https://clerk.com/docs/deployments/overview)
- [Test Mode](https://clerk.com/docs/testing/overview)
- [CLI Tools](https://clerk.com/docs/references/cli)
## Next Steps
Proceed to `clerk-sdk-patterns` for common SDK usage patterns.
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「clerk-local-dev-loop」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次