@polymorphism-tech/morph-spec 4.3.4 → 4.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/.morph/.morphversion +5 -0
  2. package/.morph/config/agents.json +948 -0
  3. package/.morph/config/config.json +9 -9
  4. package/.morph/project/context/README.md +17 -0
  5. package/.morph/project/context/detection-log.md +16 -0
  6. package/.morph/project/standards/inferred.md +59 -0
  7. package/.morph/standards/ai-agents/blazor-ui.md +364 -0
  8. package/.morph/standards/ai-agents/production.md +415 -0
  9. package/.morph/standards/ai-agents/setup.md +418 -0
  10. package/.morph/standards/ai-agents/team-orchestration.md +479 -0
  11. package/.morph/standards/ai-agents/workflows.md +354 -0
  12. package/.morph/standards/architecture/ddd/aggregates.md +120 -0
  13. package/.morph/standards/architecture/ddd/entities.md +99 -0
  14. package/.morph/standards/architecture/ddd/value-objects.md +124 -0
  15. package/.morph/standards/backend/api/minimal-api.md +494 -0
  16. package/.morph/standards/backend/api/rest.md +492 -0
  17. package/.morph/standards/backend/api/validation.md +88 -0
  18. package/.morph/standards/backend/authentication/passkeys.md +428 -0
  19. package/.morph/standards/backend/database/ef-core.md +199 -0
  20. package/.morph/standards/backend/database/migrations.md +393 -0
  21. package/.morph/standards/backend/database/postgresql/database.md +352 -0
  22. package/.morph/standards/backend/database/repository-patterns.md +528 -0
  23. package/.morph/standards/backend/database/vector-search-rag.md +541 -0
  24. package/.morph/standards/backend/dotnet/async.md +366 -0
  25. package/.morph/standards/backend/dotnet/core.md +117 -0
  26. package/.morph/standards/backend/dotnet/di.md +439 -0
  27. package/.morph/standards/backend/dotnet/program-cs-checklist.md +92 -0
  28. package/.morph/standards/backend/integrations/asaas/asaas-api.md +216 -0
  29. package/.morph/standards/backend/integrations/clerk/clerk-auth.md +290 -0
  30. package/.morph/standards/backend/integrations/hangfire/hangfire-jobs.md +350 -0
  31. package/.morph/standards/backend/integrations/resend/resend-email.md +385 -0
  32. package/.morph/standards/context/analytics.md +96 -0
  33. package/.morph/standards/context/bundles.md +110 -0
  34. package/.morph/standards/context/priming.md +78 -0
  35. package/.morph/standards/core/architecture.md +185 -0
  36. package/.morph/standards/core/coding.md +214 -0
  37. package/.morph/standards/core/git-branching-strategy.md +403 -0
  38. package/.morph/standards/core/git.md +185 -0
  39. package/.morph/standards/core/testing.md +295 -0
  40. package/.morph/standards/data/nosql/blob-storage.md +102 -0
  41. package/.morph/standards/data/nosql/cache/redis.md +97 -0
  42. package/.morph/standards/data/nosql/cosmos-db.md +118 -0
  43. package/.morph/standards/data/vector-search/azure-ai-search.md +121 -0
  44. package/.morph/standards/data/vector-search/rag-chunking.md +104 -0
  45. package/.morph/standards/frontend/blazor/design-checklist.md +222 -0
  46. package/.morph/standards/frontend/blazor/fluent-ui-setup.md +595 -0
  47. package/.morph/standards/frontend/blazor/fluent-ui.md +137 -0
  48. package/.morph/standards/frontend/blazor/html-conversion.md +184 -0
  49. package/.morph/standards/frontend/blazor/lifecycle.md +195 -0
  50. package/.morph/standards/frontend/blazor/pitfalls.md +198 -0
  51. package/.morph/standards/frontend/blazor/state.md +191 -0
  52. package/.morph/standards/frontend/design-system/animations.md +151 -0
  53. package/.morph/standards/frontend/design-system/naming.md +64 -0
  54. package/.morph/standards/frontend/nextjs/nextjs-patterns.md +198 -0
  55. package/.morph/standards/infrastructure/azure/azure.md +624 -0
  56. package/.morph/standards/infrastructure/azure/bicep/bicep-patterns.md +422 -0
  57. package/.morph/standards/infrastructure/azure/devops/azure-devops-setup.md +516 -0
  58. package/.morph/standards/infrastructure/azure/devops/local-development.md +520 -0
  59. package/.morph/standards/infrastructure/azure/services/functions.md +486 -0
  60. package/.morph/standards/infrastructure/azure/services/service-bus.md +459 -0
  61. package/.morph/standards/infrastructure/azure/services/storage.md +407 -0
  62. package/.morph/standards/infrastructure/docker/easypanel-deploy.md +196 -0
  63. package/.morph/standards/infrastructure/supabase/mcp-setup.md +252 -0
  64. package/.morph/standards/infrastructure/supabase/supabase-auth.md +176 -0
  65. package/.morph/standards/infrastructure/supabase/supabase-pgvector.md +169 -0
  66. package/.morph/standards/infrastructure/supabase/supabase-rls.md +184 -0
  67. package/.morph/standards/infrastructure/supabase/supabase-storage.md +153 -0
  68. package/.morph/standards/integration/api/graphql.md +91 -0
  69. package/.morph/standards/integration/api/grpc.md +114 -0
  70. package/.morph/standards/integration/api/rest-design.md +95 -0
  71. package/.morph/standards/integration/event-driven/cqrs.md +101 -0
  72. package/.morph/standards/integration/event-driven/event-sourcing.md +124 -0
  73. package/.morph/standards/integration/event-driven/service-bus.md +95 -0
  74. package/.morph/standards/observability/logging.md +131 -0
  75. package/.morph/standards/observability/metrics.md +121 -0
  76. package/.morph/standards/observability/monitoring.md +114 -0
  77. package/.morph/standards/observability/tracing.md +132 -0
  78. package/.morph/standards/workflows/parallel-execution.md +112 -0
  79. package/.morph/standards/workflows/thread-management.md +113 -0
  80. package/.morph/templates/.idea/morph-templates.xml +92 -0
  81. package/.morph/templates/.vscode/morph-templates.code-snippets +186 -0
  82. package/.morph/templates/IDE-SNIPPETS.md +266 -0
  83. package/.morph/templates/README.md +814 -0
  84. package/.morph/templates/REGISTRY.json +1677 -0
  85. package/.morph/templates/code/dotnet/backend/repository.cs +141 -0
  86. package/.morph/templates/code/dotnet/backend/service.cs +139 -0
  87. package/.morph/templates/code/dotnet/contracts/Commands.cs +74 -0
  88. package/.morph/templates/code/dotnet/contracts/Entities.cs +25 -0
  89. package/.morph/templates/code/dotnet/contracts/Queries.cs +74 -0
  90. package/.morph/templates/code/dotnet/contracts/README.md +74 -0
  91. package/.morph/templates/code/dotnet/contracts/api-contracts.cs +173 -0
  92. package/.morph/templates/code/dotnet/contracts/contracts.cs +217 -0
  93. package/.morph/templates/code/dotnet/database/migration.cs +83 -0
  94. package/.morph/templates/code/dotnet/frontend/component.razor +239 -0
  95. package/.morph/templates/code/dotnet/jobs/agent.cs +163 -0
  96. package/.morph/templates/code/dotnet/jobs/job.cs +171 -0
  97. package/.morph/templates/code/dotnet/test.cs +239 -0
  98. package/.morph/templates/code/sql/rls-policy.sql +57 -0
  99. package/.morph/templates/code/sql/supabase-migration.sql +100 -0
  100. package/.morph/templates/code/sql/supabase-migration.template.sql +113 -0
  101. package/.morph/templates/code/typescript/contracts.ts +168 -0
  102. package/.morph/templates/context/CONTEXT-FEATURE.md +276 -0
  103. package/.morph/templates/context/CONTEXT.md +181 -0
  104. package/.morph/templates/docs/proposal.md +182 -0
  105. package/.morph/templates/docs/spec.md +149 -0
  106. package/.morph/templates/examples/design-system-examples.md +357 -0
  107. package/.morph/templates/examples/spec-examples.md +90 -0
  108. package/.morph/templates/feature/decisions.md +187 -0
  109. package/.morph/templates/feature/recap.md +146 -0
  110. package/.morph/templates/feature/tasks.md +199 -0
  111. package/.morph/templates/infrastructure/azure/Dockerfile.example +82 -0
  112. package/.morph/templates/infrastructure/azure/README.md +286 -0
  113. package/.morph/templates/infrastructure/azure/app-insights.bicep +63 -0
  114. package/.morph/templates/infrastructure/azure/app-service.bicep +164 -0
  115. package/.morph/templates/infrastructure/azure/container-app-env.bicep +49 -0
  116. package/.morph/templates/infrastructure/azure/container-app.bicep +156 -0
  117. package/.morph/templates/infrastructure/azure/deploy-checklist.md +426 -0
  118. package/.morph/templates/infrastructure/azure/deploy.ps1 +229 -0
  119. package/.morph/templates/infrastructure/azure/deploy.sh +208 -0
  120. package/.morph/templates/infrastructure/azure/key-vault.bicep +91 -0
  121. package/.morph/templates/infrastructure/azure/main.bicep +189 -0
  122. package/.morph/templates/infrastructure/azure/parameters.dev.json +29 -0
  123. package/.morph/templates/infrastructure/azure/parameters.prod.json +29 -0
  124. package/.morph/templates/infrastructure/azure/parameters.staging.json +29 -0
  125. package/.morph/templates/infrastructure/azure/sql-database.bicep +103 -0
  126. package/.morph/templates/infrastructure/azure/storage.bicep +106 -0
  127. package/.morph/templates/infrastructure/docker/Dockerfile.template +58 -0
  128. package/.morph/templates/infrastructure/docker/docker-compose.template.yml +67 -0
  129. package/.morph/templates/infrastructure/docker/dockerfile-api.dockerfile +38 -0
  130. package/.morph/templates/infrastructure/docker/dockerfile-web.dockerfile +48 -0
  131. package/.morph/templates/infrastructure/docker/easypanel.template.json +54 -0
  132. package/.morph/templates/infrastructure/github/README.md +593 -0
  133. package/.morph/templates/infrastructure/github/actions/azure-auth/action.yml.hbs +22 -0
  134. package/.morph/templates/infrastructure/github/actions/docker-build-push/action.yml.hbs +45 -0
  135. package/.morph/templates/infrastructure/github/actions/health-check/action.yml.hbs +27 -0
  136. package/.morph/templates/infrastructure/github/workflows/deploy-azure-app-service.yml.hbs +61 -0
  137. package/.morph/templates/infrastructure/github/workflows/deploy-easypanel.yml.hbs +31 -0
  138. package/.morph/templates/infrastructure/github/workflows/docker-build-push.yml.hbs +59 -0
  139. package/.morph/templates/infrastructure/github/workflows/dotnet-build.yml.hbs +39 -0
  140. package/.morph/templates/integrations/asaas-client.cs +387 -0
  141. package/.morph/templates/integrations/asaas-webhook.cs +351 -0
  142. package/.morph/templates/integrations/azure-identity-config.cs +288 -0
  143. package/.morph/templates/integrations/clerk-config.cs +258 -0
  144. package/.morph/templates/meta-prompts/fusion/fusion-agent.md +76 -0
  145. package/.morph/templates/meta-prompts/fusion/fusion-aggregator.md +100 -0
  146. package/.morph/templates/meta-prompts/hops/hop-retry.md +78 -0
  147. package/.morph/templates/meta-prompts/hops/hop-validation.md +97 -0
  148. package/.morph/templates/meta-prompts/hops/hop-wrapper.md +36 -0
  149. package/.morph/templates/meta-prompts/parallel-workers/parallel-coordinator.md +113 -0
  150. package/.morph/templates/meta-prompts/parallel-workers/parallel-worker.md +80 -0
  151. package/.morph/templates/meta-prompts/squad-leaders/backend-squad.md +90 -0
  152. package/.morph/templates/meta-prompts/squad-leaders/frontend-squad.md +126 -0
  153. package/.morph/templates/meta-prompts/squad-leaders/squad-leader.md +43 -0
  154. package/.morph/templates/meta-prompts/validators/checkpoint-validator.md +107 -0
  155. package/.morph/templates/meta-prompts/validators/pre-commit-validator.md +95 -0
  156. package/.morph/templates/saas/subscription.cs +347 -0
  157. package/.morph/templates/saas/tenant.cs +338 -0
  158. package/.morph/templates/state.template.json +17 -0
  159. package/.morph/templates/ui/FluentDesignTheme.cs +149 -0
  160. package/.morph/templates/ui/MudTheme.cs +281 -0
  161. package/.morph/templates/ui/design-system.css +226 -0
  162. package/bin/morph-spec.js +1 -1
  163. package/package.json +1 -1
  164. package/src/commands/project/update.js +185 -46
@@ -0,0 +1,216 @@
1
+ # Asaas API Integration Standard
2
+
3
+ > **Scope:** universal
4
+ > **Layer:** 2 (on keyword)
5
+ > **Keywords:** asaas, payment, brazilian, pix, boleto, billing
6
+ > **Load When:** asaas or brazilian payment keywords detected
7
+
8
+ Brazilian payment gateway integration for PIX, Boleto, and credit card processing.
9
+
10
+ ---
11
+
12
+ ## Overview
13
+
14
+ Asaas is a Brazilian payment service provider offering:
15
+ - PIX instant payments
16
+ - Boleto bancário
17
+ - Credit card processing
18
+ - Subscription billing
19
+ - Split payments
20
+
21
+ **Stack:** .NET Backend
22
+
23
+ ---
24
+
25
+ ## Core Principles
26
+
27
+ 1. **Webhook-First**: All payment state changes via webhooks
28
+ 2. **Idempotency**: Use idempotency keys for all POST requests
29
+ 3. **Security**: Validate webhook signatures, never store full card data
30
+ 4. **Brazil-Specific**: Handle CPF/CNPJ validation, Brazilian tax rules
31
+
32
+ ---
33
+
34
+ ## API Configuration
35
+
36
+ ### Environment Variables
37
+
38
+ ```bash
39
+ # .env.local
40
+ ASAAS_API_KEY=your_api_key_here
41
+ ASAAS_WEBHOOK_SECRET=your_webhook_secret
42
+ ASAAS_ENVIRONMENT=sandbox # or production
43
+ ```
44
+
45
+ ### TypeScript Client Setup
46
+
47
+ ```typescript
48
+ // lib/asaas/client.ts
49
+ import { createClient } from '@asaas/node-sdk';
50
+
51
+ export const asaasClient = createClient({
52
+ apiKey: process.env.ASAAS_API_KEY!,
53
+ environment: process.env.ASAAS_ENVIRONMENT as 'sandbox' | 'production'
54
+ });
55
+ ```
56
+
57
+ ---
58
+
59
+ ## Common Operations
60
+
61
+ ### Create PIX Payment
62
+
63
+ ```typescript
64
+ // app/api/payments/pix/route.ts
65
+ import { asaasClient } from '@/lib/asaas/client';
66
+
67
+ export async function POST(req: Request) {
68
+ const { amount, customerId, description } = await req.json();
69
+
70
+ const payment = await asaasClient.payments.create({
71
+ customer: customerId,
72
+ billingType: 'PIX',
73
+ value: amount,
74
+ dueDate: new Date().toISOString().split('T')[0],
75
+ description,
76
+ });
77
+
78
+ return Response.json({
79
+ paymentId: payment.id,
80
+ pixCode: payment.pixTransaction.payload,
81
+ qrCode: payment.pixTransaction.qrCode.encodedImage
82
+ });
83
+ }
84
+ ```
85
+
86
+ ### Webhook Handler
87
+
88
+ ```typescript
89
+ // app/api/webhooks/asaas/route.ts
90
+ import { headers } from 'next/headers';
91
+ import crypto from 'crypto';
92
+
93
+ export async function POST(req: Request) {
94
+ const body = await req.text();
95
+ const signature = headers().get('asaas-signature');
96
+
97
+ // Verify webhook signature
98
+ const expectedSignature = crypto
99
+ .createHmac('sha256', process.env.ASAAS_WEBHOOK_SECRET!)
100
+ .update(body)
101
+ .digest('hex');
102
+
103
+ if (signature !== expectedSignature) {
104
+ return new Response('Invalid signature', { status: 401 });
105
+ }
106
+
107
+ const event = JSON.parse(body);
108
+
109
+ // Handle payment events
110
+ switch (event.event) {
111
+ case 'PAYMENT_RECEIVED':
112
+ await handlePaymentReceived(event.payment);
113
+ break;
114
+ case 'PAYMENT_CONFIRMED':
115
+ await handlePaymentConfirmed(event.payment);
116
+ break;
117
+ }
118
+
119
+ return new Response('OK');
120
+ }
121
+ ```
122
+
123
+ ---
124
+
125
+ ## Database Schema (Supabase)
126
+
127
+ ```sql
128
+ -- Store payment records
129
+ create table payments (
130
+ id uuid primary key default gen_random_uuid(),
131
+ user_id uuid references auth.users(id),
132
+ asaas_payment_id text unique not null,
133
+ amount decimal(10,2) not null,
134
+ status text not null, -- pending, confirmed, received
135
+ billing_type text not null, -- PIX, BOLETO, CREDIT_CARD
136
+ created_at timestamptz default now(),
137
+ updated_at timestamptz default now()
138
+ );
139
+
140
+ -- RLS policies
141
+ alter table payments enable row level security;
142
+
143
+ create policy "Users read own payments"
144
+ on payments for select
145
+ using (auth.uid() = user_id);
146
+ ```
147
+
148
+ ---
149
+
150
+ ## Best Practices
151
+
152
+ ### CPF/CNPJ Validation
153
+
154
+ ```typescript
155
+ export function validateCPF(cpf: string): boolean {
156
+ cpf = cpf.replace(/[^\d]/g, '');
157
+ if (cpf.length !== 11) return false;
158
+
159
+ // Validation logic...
160
+ return true;
161
+ }
162
+
163
+ export function validateCNPJ(cnpj: string): boolean {
164
+ cnpj = cnpj.replace(/[^\d]/g, '');
165
+ if (cnpj.length !== 14) return false;
166
+
167
+ // Validation logic...
168
+ return true;
169
+ }
170
+ ```
171
+
172
+ ### Idempotency Keys
173
+
174
+ ```typescript
175
+ const idempotencyKey = `payment-${userId}-${Date.now()}`;
176
+
177
+ const payment = await asaasClient.payments.create({
178
+ // ... payment data
179
+ }, {
180
+ headers: {
181
+ 'Idempotency-Key': idempotencyKey
182
+ }
183
+ });
184
+ ```
185
+
186
+ ---
187
+
188
+ ## Error Handling
189
+
190
+ ```typescript
191
+ try {
192
+ const payment = await asaasClient.payments.create(data);
193
+ } catch (error) {
194
+ if (error.code === 'invalid_cpf') {
195
+ return Response.json({ error: 'CPF inválido' }, { status: 400 });
196
+ }
197
+ if (error.code === 'insufficient_balance') {
198
+ return Response.json({ error: 'Saldo insuficiente' }, { status: 402 });
199
+ }
200
+
201
+ console.error('Asaas API error:', error);
202
+ return Response.json({ error: 'Erro no processamento' }, { status: 500 });
203
+ }
204
+ ```
205
+
206
+ ---
207
+
208
+ ## References
209
+
210
+ - [Asaas API Documentation](https://docs.asaas.com/)
211
+ - [Asaas Node SDK](https://github.com/asaas/asaas-node)
212
+ - PIX Specification: Brazilian Central Bank
213
+
214
+ ---
215
+
216
+ *MORPH-SPEC by Polymorphism Tech*
@@ -0,0 +1,290 @@
1
+ # Clerk Authentication Integration Standard
2
+
3
+ > **Scope:** universal
4
+ > **Layer:** 2 (on keyword)
5
+ > **Keywords:** clerk, auth, authentication, nextjs, user management
6
+ > **Load When:** clerk authentication keywords detected
7
+
8
+ Modern authentication and user management for Next.js applications.
9
+
10
+ ---
11
+
12
+ ## Overview
13
+
14
+ Clerk provides:
15
+ - Pre-built UI components (SignIn, SignUp, UserProfile)
16
+ - Social auth (Google, GitHub, etc.)
17
+ - Multi-factor authentication
18
+ - Session management
19
+ - User metadata and organizations
20
+
21
+ **Primary Use Case:** Next.js 15 + App Router
22
+
23
+ ---
24
+
25
+ ## Core Principles
26
+
27
+ 1. **Server-First**: Use server-side auth checks with `auth()` helper
28
+ 2. **Route Protection**: Middleware-based protection for private routes
29
+ 3. **Type Safety**: Use Clerk's TypeScript types
30
+ 4. **Edge Compatible**: Works with Edge Runtime
31
+
32
+ ---
33
+
34
+ ## Installation & Setup
35
+
36
+ ### Install Packages
37
+
38
+ ```bash
39
+ npm install @clerk/nextjs
40
+ ```
41
+
42
+ ### Environment Variables
43
+
44
+ ```bash
45
+ # .env.local
46
+ NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_...
47
+ CLERK_SECRET_KEY=sk_test_...
48
+ NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
49
+ NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up
50
+ NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL=/dashboard
51
+ NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/onboarding
52
+ ```
53
+
54
+ ### Root Layout Wrapper
55
+
56
+ ```typescript
57
+ // app/layout.tsx
58
+ import { ClerkProvider } from '@clerk/nextjs';
59
+
60
+ export default function RootLayout({ children }: { children: React.ReactNode }) {
61
+ return (
62
+ <ClerkProvider>
63
+ <html lang="en">
64
+ <body>{children}</body>
65
+ </html>
66
+ </ClerkProvider>
67
+ );
68
+ }
69
+ ```
70
+
71
+ ### Middleware Protection
72
+
73
+ ```typescript
74
+ // middleware.ts
75
+ import { authMiddleware } from '@clerk/nextjs';
76
+
77
+ export default authMiddleware({
78
+ publicRoutes: ['/', '/api/webhooks/clerk'],
79
+ ignoredRoutes: ['/api/public']
80
+ });
81
+
82
+ export const config = {
83
+ matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'],
84
+ };
85
+ ```
86
+
87
+ ---
88
+
89
+ ## Common Patterns
90
+
91
+ ### Sign In Page
92
+
93
+ ```typescript
94
+ // app/sign-in/[[...sign-in]]/page.tsx
95
+ import { SignIn } from '@clerk/nextjs';
96
+
97
+ export default function SignInPage() {
98
+ return (
99
+ <div className="flex items-center justify-center min-h-screen">
100
+ <SignIn />
101
+ </div>
102
+ );
103
+ }
104
+ ```
105
+
106
+ ### Server-Side Auth Check
107
+
108
+ ```typescript
109
+ // app/dashboard/page.tsx
110
+ import { auth } from '@clerk/nextjs';
111
+ import { redirect } from 'next/navigation';
112
+
113
+ export default async function DashboardPage() {
114
+ const { userId } = auth();
115
+
116
+ if (!userId) {
117
+ redirect('/sign-in');
118
+ }
119
+
120
+ return <div>Dashboard for user {userId}</div>;
121
+ }
122
+ ```
123
+
124
+ ### Client Component with User Data
125
+
126
+ ```typescript
127
+ // components/UserProfile.tsx
128
+ 'use client';
129
+ import { useUser } from '@clerk/nextjs';
130
+
131
+ export function UserProfile() {
132
+ const { user, isLoaded, isSignedIn } = useUser();
133
+
134
+ if (!isLoaded) return <div>Loading...</div>;
135
+ if (!isSignedIn) return <div>Not signed in</div>;
136
+
137
+ return (
138
+ <div>
139
+ <p>Welcome, {user.firstName}!</p>
140
+ <p>Email: {user.primaryEmailAddress?.emailAddress}</p>
141
+ </div>
142
+ );
143
+ }
144
+ ```
145
+
146
+ ### API Route Protection
147
+
148
+ ```typescript
149
+ // app/api/protected/route.ts
150
+ import { auth } from '@clerk/nextjs';
151
+
152
+ export async function GET() {
153
+ const { userId } = auth();
154
+
155
+ if (!userId) {
156
+ return new Response('Unauthorized', { status: 401 });
157
+ }
158
+
159
+ // Fetch user-specific data
160
+ const data = await fetchUserData(userId);
161
+
162
+ return Response.json(data);
163
+ }
164
+ ```
165
+
166
+ ---
167
+
168
+ ## Webhook Integration
169
+
170
+ ### Setup Webhook Endpoint
171
+
172
+ ```typescript
173
+ // app/api/webhooks/clerk/route.ts
174
+ import { Webhook } from 'svix';
175
+ import { headers } from 'next/headers';
176
+
177
+ export async function POST(req: Request) {
178
+ const WEBHOOK_SECRET = process.env.CLERK_WEBHOOK_SECRET!;
179
+
180
+ const headerPayload = headers();
181
+ const svix_id = headerPayload.get('svix-id');
182
+ const svix_timestamp = headerPayload.get('svix-timestamp');
183
+ const svix_signature = headerPayload.get('svix-signature');
184
+
185
+ if (!svix_id || !svix_timestamp || !svix_signature) {
186
+ return new Response('Missing svix headers', { status: 400 });
187
+ }
188
+
189
+ const payload = await req.json();
190
+ const body = JSON.stringify(payload);
191
+
192
+ const wh = new Webhook(WEBHOOK_SECRET);
193
+
194
+ let evt;
195
+ try {
196
+ evt = wh.verify(body, {
197
+ 'svix-id': svix_id,
198
+ 'svix-timestamp': svix_timestamp,
199
+ 'svix-signature': svix_signature,
200
+ });
201
+ } catch (err) {
202
+ return new Response('Invalid signature', { status: 400 });
203
+ }
204
+
205
+ // Handle events
206
+ switch (evt.type) {
207
+ case 'user.created':
208
+ await handleUserCreated(evt.data);
209
+ break;
210
+ case 'user.updated':
211
+ await handleUserUpdated(evt.data);
212
+ break;
213
+ case 'user.deleted':
214
+ await handleUserDeleted(evt.data);
215
+ break;
216
+ }
217
+
218
+ return new Response('OK');
219
+ }
220
+ ```
221
+
222
+ ---
223
+
224
+ ## Supabase Integration
225
+
226
+ ### Sync Users to Supabase
227
+
228
+ ```typescript
229
+ // lib/clerk/sync-user.ts
230
+ import { createClient } from '@supabase/supabase-js';
231
+
232
+ async function handleUserCreated(userData: any) {
233
+ const supabase = createClient(
234
+ process.env.NEXT_PUBLIC_SUPABASE_URL!,
235
+ process.env.SUPABASE_SERVICE_ROLE_KEY!
236
+ );
237
+
238
+ await supabase.from('profiles').insert({
239
+ clerk_user_id: userData.id,
240
+ email: userData.email_addresses[0].email_address,
241
+ first_name: userData.first_name,
242
+ last_name: userData.last_name,
243
+ avatar_url: userData.image_url
244
+ });
245
+ }
246
+ ```
247
+
248
+ ---
249
+
250
+ ## Best Practices
251
+
252
+ ### Custom Claims
253
+
254
+ ```typescript
255
+ // Add custom claims in Clerk Dashboard or via API
256
+ const { sessionClaims } = auth();
257
+ const role = sessionClaims?.metadata?.role as string;
258
+
259
+ if (role !== 'admin') {
260
+ return new Response('Forbidden', { status: 403 });
261
+ }
262
+ ```
263
+
264
+ ### Organizations
265
+
266
+ ```typescript
267
+ import { auth } from '@clerk/nextjs';
268
+
269
+ const { orgId, orgRole } = auth();
270
+
271
+ if (!orgId) {
272
+ redirect('/create-organization');
273
+ }
274
+
275
+ if (orgRole !== 'admin') {
276
+ return <div>Admin access required</div>;
277
+ }
278
+ ```
279
+
280
+ ---
281
+
282
+ ## References
283
+
284
+ - [Clerk Next.js Documentation](https://clerk.com/docs/nextjs)
285
+ - [Clerk API Reference](https://clerk.com/docs/reference/backend-api)
286
+ - [Webhooks Guide](https://clerk.com/docs/integrations/webhooks)
287
+
288
+ ---
289
+
290
+ *MORPH-SPEC by Polymorphism Tech*