@kood/claude-code 0.3.7 → 0.3.9
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.
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/templates/.claude/agents/code-reviewer.md +138 -123
- package/templates/.claude/agents/dependency-manager.md +99 -84
- package/templates/.claude/agents/deployment-validator.md +70 -55
- package/templates/.claude/agents/git-operator.md +78 -63
- package/templates/.claude/agents/implementation-executor.md +109 -94
- package/templates/.claude/agents/ko-to-en-translator.md +74 -0
- package/templates/.claude/agents/lint-fixer.md +93 -78
- package/templates/.claude/agents/refactor-advisor.md +136 -121
- package/templates/.claude/commands/agent-creator.md +199 -184
- package/templates/.claude/commands/bug-fix.md +207 -192
- package/templates/.claude/commands/command-creator.md +69 -53
- package/templates/.claude/commands/docs-creator.md +72 -56
- package/templates/.claude/commands/docs-refactor.md +41 -25
- package/templates/.claude/commands/execute.md +27 -11
- package/templates/.claude/commands/git-all.md +46 -31
- package/templates/.claude/commands/git-session.md +57 -41
- package/templates/.claude/commands/git.md +49 -33
- package/templates/.claude/commands/lint-fix.md +153 -137
- package/templates/.claude/commands/lint-init.md +76 -60
- package/templates/.claude/commands/plan.md +275 -259
- package/templates/.claude/commands/prd.md +39 -23
- package/templates/.claude/commands/pre-deploy.md +124 -108
- package/templates/.claude/commands/refactor.md +162 -146
- package/templates/.claude/commands/version-update.md +32 -16
- package/templates/hono/CLAUDE.md +28 -27
- package/templates/hono/docs/architecture.md +24 -24
- package/templates/hono/docs/deployment/cloudflare.md +18 -18
- package/templates/hono/docs/deployment/docker.md +13 -13
- package/templates/hono/docs/deployment/index.md +19 -19
- package/templates/hono/docs/deployment/railway.md +32 -32
- package/templates/hono/docs/deployment/vercel.md +29 -29
- package/templates/hono/docs/guides/conventions.md +57 -57
- package/templates/hono/docs/guides/env-setup.md +47 -47
- package/templates/hono/docs/guides/getting-started.md +27 -27
- package/templates/hono/docs/library/hono/error-handling.md +11 -11
- package/templates/hono/docs/library/hono/index.md +4 -4
- package/templates/hono/docs/library/hono/middleware.md +18 -18
- package/templates/hono/docs/library/hono/rpc.md +7 -7
- package/templates/hono/docs/library/hono/validation.md +6 -6
- package/templates/hono/docs/library/prisma/cloudflare-d1.md +29 -29
- package/templates/hono/docs/library/prisma/config.md +16 -16
- package/templates/hono/docs/library/prisma/index.md +32 -32
- package/templates/hono/docs/library/t3-env/index.md +22 -22
- package/templates/hono/docs/library/zod/index.md +31 -31
- package/templates/nextjs/CLAUDE.md +54 -51
- package/templates/nextjs/docs/architecture.md +812 -0
- package/templates/nextjs/docs/design.md +183 -183
- package/templates/nextjs/docs/guides/conventions.md +86 -86
- package/templates/nextjs/docs/guides/getting-started.md +28 -28
- package/templates/nextjs/docs/guides/routes.md +32 -32
- package/templates/nextjs/docs/library/better-auth/index.md +70 -70
- package/templates/nextjs/docs/library/nextjs/app-router.md +43 -43
- package/templates/nextjs/docs/library/nextjs/caching.md +73 -73
- package/templates/nextjs/docs/library/nextjs/index.md +51 -51
- package/templates/nextjs/docs/library/nextjs/middleware.md +41 -41
- package/templates/nextjs/docs/library/nextjs/route-handlers.md +31 -31
- package/templates/nextjs/docs/library/nextjs/server-actions.md +34 -34
- package/templates/nextjs/docs/library/prisma/cloudflare-d1.md +20 -20
- package/templates/nextjs/docs/library/prisma/config.md +18 -18
- package/templates/nextjs/docs/library/prisma/crud.md +17 -17
- package/templates/nextjs/docs/library/prisma/index.md +18 -18
- package/templates/nextjs/docs/library/prisma/relations.md +16 -16
- package/templates/nextjs/docs/library/prisma/schema.md +23 -23
- package/templates/nextjs/docs/library/prisma/setup.md +6 -6
- package/templates/nextjs/docs/library/prisma/transactions.md +10 -10
- package/templates/nextjs/docs/library/tanstack-query/index.md +6 -6
- package/templates/nextjs/docs/library/tanstack-query/invalidation.md +20 -20
- package/templates/nextjs/docs/library/tanstack-query/optimistic-updates.md +4 -4
- package/templates/nextjs/docs/library/tanstack-query/use-mutation.md +15 -15
- package/templates/nextjs/docs/library/tanstack-query/use-query.md +22 -22
- package/templates/nextjs/docs/library/zod/complex-types.md +11 -11
- package/templates/nextjs/docs/library/zod/index.md +8 -8
- package/templates/nextjs/docs/library/zod/transforms.md +11 -11
- package/templates/nextjs/docs/library/zod/validation.md +9 -9
- package/templates/npx/CLAUDE.md +38 -37
- package/templates/npx/docs/library/commander/index.md +12 -12
- package/templates/npx/docs/library/fs-extra/index.md +9 -9
- package/templates/npx/docs/library/prompts/index.md +3 -3
- package/templates/npx/docs/references/patterns.md +12 -12
- package/templates/tanstack-start/CLAUDE.md +54 -53
- package/templates/tanstack-start/docs/architecture.md +128 -128
- package/templates/tanstack-start/docs/design.md +169 -169
- package/templates/tanstack-start/docs/guides/conventions.md +43 -43
- package/templates/tanstack-start/docs/guides/env-setup.md +35 -35
- package/templates/tanstack-start/docs/guides/getting-started.md +19 -19
- package/templates/tanstack-start/docs/guides/hooks.md +45 -45
- package/templates/tanstack-start/docs/guides/routes.md +54 -54
- package/templates/tanstack-start/docs/guides/services.md +45 -45
- package/templates/tanstack-start/docs/library/better-auth/index.md +68 -68
- package/templates/tanstack-start/docs/library/prisma/cloudflare-d1.md +19 -19
- package/templates/tanstack-start/docs/library/prisma/config.md +16 -16
- package/templates/tanstack-start/docs/library/prisma/crud.md +17 -17
- package/templates/tanstack-start/docs/library/prisma/index.md +17 -17
- package/templates/tanstack-start/docs/library/prisma/relations.md +16 -16
- package/templates/tanstack-start/docs/library/prisma/schema.md +23 -23
- package/templates/tanstack-start/docs/library/prisma/setup.md +6 -6
- package/templates/tanstack-start/docs/library/prisma/transactions.md +10 -10
- package/templates/tanstack-start/docs/library/t3-env/index.md +21 -160
- package/templates/tanstack-start/docs/library/tanstack-query/index.md +6 -6
- package/templates/tanstack-start/docs/library/tanstack-query/invalidation.md +19 -19
- package/templates/tanstack-start/docs/library/tanstack-query/optimistic-updates.md +4 -4
- package/templates/tanstack-start/docs/library/tanstack-query/use-mutation.md +14 -14
- package/templates/tanstack-start/docs/library/tanstack-query/use-query.md +21 -21
- package/templates/tanstack-start/docs/library/tanstack-router/error-handling.md +9 -9
- package/templates/tanstack-start/docs/library/tanstack-router/hooks.md +11 -11
- package/templates/tanstack-start/docs/library/tanstack-router/index.md +18 -18
- package/templates/tanstack-start/docs/library/tanstack-router/navigation.md +17 -17
- package/templates/tanstack-start/docs/library/tanstack-router/route-context.md +5 -5
- package/templates/tanstack-start/docs/library/tanstack-router/search-params.md +10 -10
- package/templates/tanstack-start/docs/library/tanstack-start/auth-patterns.md +8 -8
- package/templates/tanstack-start/docs/library/tanstack-start/index.md +15 -15
- package/templates/tanstack-start/docs/library/tanstack-start/middleware.md +9 -9
- package/templates/tanstack-start/docs/library/tanstack-start/routing.md +6 -6
- package/templates/tanstack-start/docs/library/tanstack-start/server-functions.md +18 -18
- package/templates/tanstack-start/docs/library/tanstack-start/setup.md +4 -4
- package/templates/tanstack-start/docs/library/zod/complex-types.md +11 -11
- package/templates/tanstack-start/docs/library/zod/index.md +8 -8
- package/templates/tanstack-start/docs/library/zod/transforms.md +11 -11
- package/templates/tanstack-start/docs/library/zod/validation.md +9 -9
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
# Routes
|
|
2
2
|
|
|
3
|
-
> Next.js App Router
|
|
3
|
+
> Next.js App Router routing patterns
|
|
4
4
|
|
|
5
5
|
---
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## Basic Routing
|
|
8
8
|
|
|
9
|
-
|
|
|
10
|
-
|
|
9
|
+
| File | Route |
|
|
10
|
+
|------|-------|
|
|
11
11
|
| `app/page.tsx` | `/` |
|
|
12
12
|
| `app/about/page.tsx` | `/about` |
|
|
13
13
|
| `app/blog/page.tsx` | `/blog` |
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
|
|
16
16
|
---
|
|
17
17
|
|
|
18
|
-
##
|
|
18
|
+
## Dynamic Routes
|
|
19
19
|
|
|
20
|
-
###
|
|
20
|
+
### Single Parameter
|
|
21
21
|
|
|
22
22
|
```typescript
|
|
23
23
|
// app/posts/[id]/page.tsx
|
|
@@ -33,7 +33,7 @@ export default async function PostPage({ params }: PageProps) {
|
|
|
33
33
|
return <article>{post.title}</article>
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
//
|
|
36
|
+
// Static generation
|
|
37
37
|
export async function generateStaticParams() {
|
|
38
38
|
const posts = await prisma.post.findMany({ select: { id: true } })
|
|
39
39
|
return posts.map(post => ({ id: post.id }))
|
|
@@ -57,9 +57,9 @@ export default function DocsPage({ params }: PageProps) {
|
|
|
57
57
|
|
|
58
58
|
---
|
|
59
59
|
|
|
60
|
-
##
|
|
60
|
+
## Layouts
|
|
61
61
|
|
|
62
|
-
### Root Layout (
|
|
62
|
+
### Root Layout (required)
|
|
63
63
|
|
|
64
64
|
```typescript
|
|
65
65
|
// app/layout.tsx
|
|
@@ -67,7 +67,7 @@ import { Providers } from "./providers"
|
|
|
67
67
|
|
|
68
68
|
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
69
69
|
return (
|
|
70
|
-
<html lang="
|
|
70
|
+
<html lang="en">
|
|
71
71
|
<body>
|
|
72
72
|
<Providers>{children}</Providers>
|
|
73
73
|
</body>
|
|
@@ -76,7 +76,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })
|
|
|
76
76
|
}
|
|
77
77
|
```
|
|
78
78
|
|
|
79
|
-
###
|
|
79
|
+
### Nested Layout
|
|
80
80
|
|
|
81
81
|
```typescript
|
|
82
82
|
// app/dashboard/layout.tsx
|
|
@@ -85,7 +85,7 @@ import { headers } from "next/headers"
|
|
|
85
85
|
import { redirect } from "next/navigation"
|
|
86
86
|
|
|
87
87
|
export default async function DashboardLayout({ children }: { children: React.ReactNode }) {
|
|
88
|
-
//
|
|
88
|
+
// Auth check
|
|
89
89
|
const session = await auth.api.getSession({ headers: headers() })
|
|
90
90
|
if (!session?.user) redirect("/login")
|
|
91
91
|
|
|
@@ -115,7 +115,7 @@ app/
|
|
|
115
115
|
└── page.tsx # /products
|
|
116
116
|
```
|
|
117
117
|
|
|
118
|
-
|
|
118
|
+
**Purpose:** Apply different layouts without affecting URLs
|
|
119
119
|
|
|
120
120
|
---
|
|
121
121
|
|
|
@@ -139,8 +139,8 @@ export default function Loading() {
|
|
|
139
139
|
export default function Error({ error, reset }: { error: Error; reset: () => void }) {
|
|
140
140
|
return (
|
|
141
141
|
<div>
|
|
142
|
-
<h2
|
|
143
|
-
<button onClick={reset}
|
|
142
|
+
<h2>Error: {error.message}</h2>
|
|
143
|
+
<button onClick={reset}>Try again</button>
|
|
144
144
|
</div>
|
|
145
145
|
)
|
|
146
146
|
}
|
|
@@ -151,7 +151,7 @@ export default function Error({ error, reset }: { error: Error; reset: () => voi
|
|
|
151
151
|
```typescript
|
|
152
152
|
// app/posts/[id]/not-found.tsx
|
|
153
153
|
export default function NotFound() {
|
|
154
|
-
return <div
|
|
154
|
+
return <div>Post not found</div>
|
|
155
155
|
}
|
|
156
156
|
```
|
|
157
157
|
|
|
@@ -159,7 +159,7 @@ export default function NotFound() {
|
|
|
159
159
|
|
|
160
160
|
## Metadata
|
|
161
161
|
|
|
162
|
-
###
|
|
162
|
+
### Static
|
|
163
163
|
|
|
164
164
|
```typescript
|
|
165
165
|
// app/about/page.tsx
|
|
@@ -175,7 +175,7 @@ export default function AboutPage() {
|
|
|
175
175
|
}
|
|
176
176
|
```
|
|
177
177
|
|
|
178
|
-
###
|
|
178
|
+
### Dynamic
|
|
179
179
|
|
|
180
180
|
```typescript
|
|
181
181
|
// app/posts/[id]/page.tsx
|
|
@@ -204,7 +204,7 @@ export async function generateMetadata({ params }: { params: { id: string } }):
|
|
|
204
204
|
|
|
205
205
|
---
|
|
206
206
|
|
|
207
|
-
##
|
|
207
|
+
## Navigation
|
|
208
208
|
|
|
209
209
|
### Link
|
|
210
210
|
|
|
@@ -242,9 +242,9 @@ export function LoginButton() {
|
|
|
242
242
|
|
|
243
243
|
---
|
|
244
244
|
|
|
245
|
-
##
|
|
245
|
+
## Auth Protection
|
|
246
246
|
|
|
247
|
-
### Layout
|
|
247
|
+
### Check in Layout
|
|
248
248
|
|
|
249
249
|
```typescript
|
|
250
250
|
// app/dashboard/layout.tsx
|
|
@@ -263,7 +263,7 @@ export default async function DashboardLayout({ children }: { children: React.Re
|
|
|
263
263
|
}
|
|
264
264
|
```
|
|
265
265
|
|
|
266
|
-
### Middleware
|
|
266
|
+
### Check in Middleware
|
|
267
267
|
|
|
268
268
|
```typescript
|
|
269
269
|
// middleware.ts
|
|
@@ -288,25 +288,25 @@ export const config = {
|
|
|
288
288
|
|
|
289
289
|
---
|
|
290
290
|
|
|
291
|
-
##
|
|
291
|
+
## Best Practices
|
|
292
292
|
|
|
293
293
|
### ✅ DO
|
|
294
294
|
|
|
295
295
|
```typescript
|
|
296
|
-
// 1.
|
|
296
|
+
// 1. Direct data fetching in Server Components
|
|
297
297
|
export default async function PostsPage() {
|
|
298
298
|
const posts = await prisma.post.findMany()
|
|
299
299
|
return <PostsList posts={posts} />
|
|
300
300
|
}
|
|
301
301
|
|
|
302
|
-
// 2.
|
|
302
|
+
// 2. Auth check in layout
|
|
303
303
|
export default async function DashboardLayout({ children }) {
|
|
304
304
|
const session = await auth.api.getSession({ headers: headers() })
|
|
305
305
|
if (!session?.user) redirect("/login")
|
|
306
306
|
return <div>{children}</div>
|
|
307
307
|
}
|
|
308
308
|
|
|
309
|
-
// 3.
|
|
309
|
+
// 3. Static generation with generateStaticParams
|
|
310
310
|
export async function generateStaticParams() {
|
|
311
311
|
const posts = await prisma.post.findMany({ select: { id: true } })
|
|
312
312
|
return posts.map(post => ({ id: post.id }))
|
|
@@ -316,26 +316,26 @@ export async function generateStaticParams() {
|
|
|
316
316
|
### ❌ DON'T
|
|
317
317
|
|
|
318
318
|
```typescript
|
|
319
|
-
// 1.
|
|
319
|
+
// 1. async/await in Client Components
|
|
320
320
|
"use client"
|
|
321
321
|
export default async function PostsPage() { // ❌
|
|
322
322
|
const posts = await prisma.post.findMany()
|
|
323
323
|
return <PostsList posts={posts} />
|
|
324
324
|
}
|
|
325
325
|
|
|
326
|
-
// 2.
|
|
326
|
+
// 2. Repeated auth checks per page
|
|
327
327
|
export default async function Page1() {
|
|
328
|
-
const session = await auth.api.getSession({ headers: headers() }) // ❌
|
|
328
|
+
const session = await auth.api.getSession({ headers: headers() }) // ❌ Duplicate
|
|
329
329
|
if (!session) redirect("/login")
|
|
330
330
|
}
|
|
331
331
|
|
|
332
|
-
// 3.
|
|
333
|
-
<Link href="/posts/123">Post</Link> // ❌
|
|
332
|
+
// 3. Hardcoded paths
|
|
333
|
+
<Link href="/posts/123">Post</Link> // ❌ Hardcoded
|
|
334
334
|
```
|
|
335
335
|
|
|
336
336
|
---
|
|
337
337
|
|
|
338
|
-
##
|
|
338
|
+
## References
|
|
339
339
|
|
|
340
340
|
- [App Router](../library/nextjs/app-router.md)
|
|
341
341
|
- [Server Actions](server-actions.md)
|
|
@@ -15,7 +15,7 @@ npm install better-auth
|
|
|
15
15
|
## Minimal Setup
|
|
16
16
|
|
|
17
17
|
```typescript
|
|
18
|
-
// src/lib/auth.ts -
|
|
18
|
+
// src/lib/auth.ts - Server
|
|
19
19
|
import { betterAuth } from "better-auth"
|
|
20
20
|
import { prismaAdapter } from "better-auth/adapters/prisma"
|
|
21
21
|
import { prisma } from "@/database/prisma"
|
|
@@ -25,7 +25,7 @@ export const auth = betterAuth({
|
|
|
25
25
|
emailAndPassword: { enabled: true },
|
|
26
26
|
})
|
|
27
27
|
|
|
28
|
-
// src/lib/auth-client.ts -
|
|
28
|
+
// src/lib/auth-client.ts - Client
|
|
29
29
|
import { createAuthClient } from "better-auth/react"
|
|
30
30
|
|
|
31
31
|
export const authClient = createAuthClient({
|
|
@@ -55,17 +55,17 @@ export const POST = (request: Request) => auth.handler(request)
|
|
|
55
55
|
|---------|--------|----------|
|
|
56
56
|
| Prisma | `better-auth/adapters/prisma` | `postgresql`, `mysql`, `sqlite` |
|
|
57
57
|
| Drizzle | `better-auth/adapters/drizzle` | `pg`, `mysql2`, `better-sqlite3` |
|
|
58
|
-
| Kysely | `better-auth/adapters/kysely` | dialect
|
|
58
|
+
| Kysely | `better-auth/adapters/kysely` | dialect-based |
|
|
59
59
|
|
|
60
60
|
## Auth Config
|
|
61
61
|
|
|
62
|
-
|
|
|
63
|
-
|
|
64
|
-
| `database` | `Adapter` |
|
|
65
|
-
| `baseURL` | `string` | `http://localhost:3000` |
|
|
66
|
-
| `basePath` | `string` | `/api/auth` | Auth
|
|
67
|
-
| `secret` | `string` |
|
|
68
|
-
| `trustedOrigins` | `string[]` | `[]` | CORS
|
|
62
|
+
| Option | Type | Default | Description |
|
|
63
|
+
|--------|------|---------|-------------|
|
|
64
|
+
| `database` | `Adapter` | required | Database adapter |
|
|
65
|
+
| `baseURL` | `string` | `http://localhost:3000` | App URL |
|
|
66
|
+
| `basePath` | `string` | `/api/auth` | Auth path |
|
|
67
|
+
| `secret` | `string` | environment variable | JWT secret |
|
|
68
|
+
| `trustedOrigins` | `string[]` | `[]` | CORS allowed origins |
|
|
69
69
|
|
|
70
70
|
## Social Providers
|
|
71
71
|
|
|
@@ -111,22 +111,22 @@ export const auth = betterAuth({
|
|
|
111
111
|
|
|
112
112
|
## Session Config
|
|
113
113
|
|
|
114
|
-
|
|
|
115
|
-
|
|
116
|
-
| `expiresIn` | `number` | `604800` (7
|
|
117
|
-
| `updateAge` | `number` | `86400` (1
|
|
118
|
-
| `cookieCache.enabled` | `boolean` | `true` |
|
|
119
|
-
| `cookieCache.maxAge` | `number` | `300` (5
|
|
120
|
-
| `cookieCache.strategy` | `'compact' \| 'jwt' \| 'jwe'` | `'compact'` |
|
|
114
|
+
| Option | Type | Default | Description |
|
|
115
|
+
|--------|------|---------|-------------|
|
|
116
|
+
| `expiresIn` | `number` | `604800` (7 days) | Session expiration time (seconds) |
|
|
117
|
+
| `updateAge` | `number` | `86400` (1 day) | Session update interval (seconds) |
|
|
118
|
+
| `cookieCache.enabled` | `boolean` | `true` | Enable cookie cache |
|
|
119
|
+
| `cookieCache.maxAge` | `number` | `300` (5 min) | Cache validity (seconds) |
|
|
120
|
+
| `cookieCache.strategy` | `'compact' \| 'jwt' \| 'jwe'` | `'compact'` | Cache strategy |
|
|
121
121
|
|
|
122
122
|
```typescript
|
|
123
123
|
export const auth = betterAuth({
|
|
124
124
|
session: {
|
|
125
|
-
expiresIn: 604800, // 7
|
|
126
|
-
updateAge: 86400, // 1
|
|
125
|
+
expiresIn: 604800, // 7 days
|
|
126
|
+
updateAge: 86400, // Update every 1 day
|
|
127
127
|
cookieCache: {
|
|
128
128
|
enabled: true,
|
|
129
|
-
maxAge: 300, // 5
|
|
129
|
+
maxAge: 300, // 5 minutes
|
|
130
130
|
strategy: "compact",
|
|
131
131
|
},
|
|
132
132
|
},
|
|
@@ -136,11 +136,11 @@ export const auth = betterAuth({
|
|
|
136
136
|
## Session Methods
|
|
137
137
|
|
|
138
138
|
```typescript
|
|
139
|
-
// ✅
|
|
139
|
+
// ✅ Client
|
|
140
140
|
const session = await authClient.getSession()
|
|
141
141
|
const session = await authClient.getSession({ query: { disableCookieCache: true } })
|
|
142
142
|
|
|
143
|
-
// ✅
|
|
143
|
+
// ✅ Server (Server Component)
|
|
144
144
|
import { auth } from "@/lib/auth"
|
|
145
145
|
import { headers } from "next/headers"
|
|
146
146
|
|
|
@@ -151,7 +151,7 @@ export default async function Page() {
|
|
|
151
151
|
return <div>Welcome {session.user.name}</div>
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
-
// ✅
|
|
154
|
+
// ✅ Server (Server Action)
|
|
155
155
|
"use server"
|
|
156
156
|
|
|
157
157
|
import { auth } from "@/lib/auth"
|
|
@@ -162,10 +162,10 @@ export async function getSession() {
|
|
|
162
162
|
return session
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
-
// ✅
|
|
165
|
+
// ✅ Update session
|
|
166
166
|
await authClient.updateUser({ name: "New Name" })
|
|
167
167
|
|
|
168
|
-
// ✅
|
|
168
|
+
// ✅ Logout
|
|
169
169
|
await authClient.signOut()
|
|
170
170
|
```
|
|
171
171
|
|
|
@@ -207,30 +207,30 @@ export const auth = betterAuth({
|
|
|
207
207
|
## Email/Password
|
|
208
208
|
|
|
209
209
|
```typescript
|
|
210
|
-
// ✅
|
|
210
|
+
// ✅ Sign up
|
|
211
211
|
await authClient.signUp.email({
|
|
212
212
|
email: "user@example.com",
|
|
213
213
|
password: "password123",
|
|
214
214
|
name: "User Name",
|
|
215
215
|
})
|
|
216
216
|
|
|
217
|
-
// ✅
|
|
217
|
+
// ✅ Sign in
|
|
218
218
|
await authClient.signIn.email({
|
|
219
219
|
email: "user@example.com",
|
|
220
220
|
password: "password123",
|
|
221
221
|
})
|
|
222
222
|
|
|
223
|
-
// ✅
|
|
223
|
+
// ✅ Request password reset
|
|
224
224
|
await authClient.forgetPassword({ email: "user@example.com" })
|
|
225
225
|
|
|
226
|
-
// ✅
|
|
226
|
+
// ✅ Reset password
|
|
227
227
|
await authClient.resetPassword({ token, password: "newpassword" })
|
|
228
228
|
```
|
|
229
229
|
|
|
230
230
|
## Social Login
|
|
231
231
|
|
|
232
232
|
```typescript
|
|
233
|
-
// ✅
|
|
233
|
+
// ✅ Social sign in
|
|
234
234
|
await authClient.signIn.social({ provider: "google", callbackURL: "/dashboard" })
|
|
235
235
|
await authClient.signIn.social({ provider: "github", callbackURL: "/dashboard" })
|
|
236
236
|
|
|
@@ -257,8 +257,8 @@ export const auth = betterAuth({
|
|
|
257
257
|
html: `Your code: ${otp}`,
|
|
258
258
|
})
|
|
259
259
|
},
|
|
260
|
-
period: 300, // 5
|
|
261
|
-
length: 6, // 6
|
|
260
|
+
period: 300, // 5 minutes
|
|
261
|
+
length: 6, // 6 digits
|
|
262
262
|
},
|
|
263
263
|
backupCodeLength: 10,
|
|
264
264
|
backupCodeCount: 10,
|
|
@@ -284,26 +284,26 @@ export const authClient = createAuthClient({
|
|
|
284
284
|
### 2FA Usage
|
|
285
285
|
|
|
286
286
|
```typescript
|
|
287
|
-
// ✅ TOTP
|
|
287
|
+
// ✅ Enable TOTP
|
|
288
288
|
const { data } = await authClient.twoFactor.enable({ password: "current-password" })
|
|
289
289
|
// data: { totpURI: 'otpauth://...', backupCodes: ['ABCD-1234', ...] }
|
|
290
290
|
|
|
291
|
-
// ✅ TOTP
|
|
291
|
+
// ✅ Verify TOTP
|
|
292
292
|
await authClient.twoFactor.verifyTotp({ code: "123456" })
|
|
293
293
|
|
|
294
|
-
// ✅ OTP
|
|
294
|
+
// ✅ Send OTP
|
|
295
295
|
await authClient.twoFactor.sendOtp()
|
|
296
296
|
|
|
297
|
-
// ✅ OTP
|
|
297
|
+
// ✅ Verify OTP
|
|
298
298
|
await authClient.twoFactor.verifyOtp({ code: "123456" })
|
|
299
299
|
|
|
300
|
-
// ✅
|
|
300
|
+
// ✅ Use backup code
|
|
301
301
|
await authClient.twoFactor.useBackupCode({ code: "ABCD-1234" })
|
|
302
302
|
|
|
303
|
-
// ✅
|
|
303
|
+
// ✅ Regenerate backup codes
|
|
304
304
|
const { data } = await authClient.twoFactor.regenerateBackupCodes({ password: "current-password" })
|
|
305
305
|
|
|
306
|
-
// ✅ 2FA
|
|
306
|
+
// ✅ Disable 2FA
|
|
307
307
|
await authClient.twoFactor.disable({ password: "current-password" })
|
|
308
308
|
```
|
|
309
309
|
|
|
@@ -315,35 +315,35 @@ await authClient.twoFactor.disable({ password: "current-password" })
|
|
|
315
315
|
|
|
316
316
|
## Plugin System
|
|
317
317
|
|
|
318
|
-
| Plugin | Import |
|
|
319
|
-
|
|
320
|
-
| `multiSession` | `better-auth/plugins` |
|
|
321
|
-
| `customSession` | `better-auth/plugins` |
|
|
322
|
-
| `twoFactor` | `better-auth/plugins` |
|
|
323
|
-
| `captcha` | `better-auth/plugins` | CAPTCHA
|
|
318
|
+
| Plugin | Import | Feature |
|
|
319
|
+
|--------|--------|---------|
|
|
320
|
+
| `multiSession` | `better-auth/plugins` | Multi-session management |
|
|
321
|
+
| `customSession` | `better-auth/plugins` | Extend session fields |
|
|
322
|
+
| `twoFactor` | `better-auth/plugins` | Two-factor authentication |
|
|
323
|
+
| `captcha` | `better-auth/plugins` | CAPTCHA validation |
|
|
324
324
|
|
|
325
325
|
## Multi-Session
|
|
326
326
|
|
|
327
327
|
```typescript
|
|
328
|
-
// ✅
|
|
328
|
+
// ✅ Server
|
|
329
329
|
import { multiSession } from "better-auth/plugins"
|
|
330
330
|
|
|
331
331
|
export const auth = betterAuth({
|
|
332
332
|
plugins: [
|
|
333
333
|
multiSession({
|
|
334
|
-
maximumSessions: 5, //
|
|
334
|
+
maximumSessions: 5, // Maximum sessions
|
|
335
335
|
}),
|
|
336
336
|
],
|
|
337
337
|
})
|
|
338
338
|
|
|
339
|
-
// ✅
|
|
339
|
+
// ✅ Client
|
|
340
340
|
import { multiSessionClient } from "better-auth/client/plugins"
|
|
341
341
|
|
|
342
342
|
export const authClient = createAuthClient({
|
|
343
343
|
plugins: [multiSessionClient()],
|
|
344
344
|
})
|
|
345
345
|
|
|
346
|
-
//
|
|
346
|
+
// Usage
|
|
347
347
|
const sessions = await authClient.multiSession.listSessions()
|
|
348
348
|
await authClient.multiSession.revokeSession({ sessionId: "session-id" })
|
|
349
349
|
await authClient.multiSession.revokeOtherSessions()
|
|
@@ -352,7 +352,7 @@ await authClient.multiSession.revokeOtherSessions()
|
|
|
352
352
|
## CAPTCHA
|
|
353
353
|
|
|
354
354
|
```typescript
|
|
355
|
-
// ✅
|
|
355
|
+
// ✅ Server
|
|
356
356
|
import { captcha } from "better-auth/plugins"
|
|
357
357
|
|
|
358
358
|
export const auth = betterAuth({
|
|
@@ -366,7 +366,7 @@ export const auth = betterAuth({
|
|
|
366
366
|
],
|
|
367
367
|
})
|
|
368
368
|
|
|
369
|
-
// ✅
|
|
369
|
+
// ✅ Client
|
|
370
370
|
import { captchaClient } from "better-auth/client/plugins"
|
|
371
371
|
|
|
372
372
|
export const authClient = createAuthClient({
|
|
@@ -387,8 +387,8 @@ export const authClient = createAuthClient({
|
|
|
387
387
|
## SIWE (Ethereum)
|
|
388
388
|
|
|
389
389
|
```typescript
|
|
390
|
-
//
|
|
391
|
-
//
|
|
390
|
+
// Server: siwe({ domain, uri })
|
|
391
|
+
// Client:
|
|
392
392
|
const { data } = await authClient.siwe.getNonce()
|
|
393
393
|
const message = await authClient.siwe.prepareMessage({ address: "0x...", nonce: data.nonce })
|
|
394
394
|
const signature = await signer.signMessage(message)
|
|
@@ -398,7 +398,7 @@ await authClient.siwe.signIn({ message, signature })
|
|
|
398
398
|
## Stateless Mode
|
|
399
399
|
|
|
400
400
|
```typescript
|
|
401
|
-
// ✅
|
|
401
|
+
// ✅ Social login only without DB
|
|
402
402
|
export const auth = betterAuth({
|
|
403
403
|
socialProviders: {
|
|
404
404
|
google: {
|
|
@@ -441,8 +441,8 @@ export const auth = betterAuth({
|
|
|
441
441
|
|
|
442
442
|
## Next.js Integration
|
|
443
443
|
|
|
444
|
-
|
|
|
445
|
-
|
|
444
|
+
| Pattern | Usage |
|
|
445
|
+
|---------|-------|
|
|
446
446
|
| Route Handler | `app/api/auth/[...all]/route.ts` |
|
|
447
447
|
| Server Component | `auth.api.getSession({ headers })` |
|
|
448
448
|
| Server Action | `auth.api.getSession({ headers })` |
|
|
@@ -518,24 +518,24 @@ export const config = {
|
|
|
518
518
|
|
|
519
519
|
## Common Patterns
|
|
520
520
|
|
|
521
|
-
|
|
|
522
|
-
|
|
523
|
-
|
|
|
524
|
-
|
|
|
525
|
-
|
|
|
526
|
-
|
|
|
527
|
-
|
|
|
528
|
-
|
|
|
529
|
-
|
|
|
521
|
+
| Task | Pattern |
|
|
522
|
+
|------|---------|
|
|
523
|
+
| Sign in | `authClient.signIn.email({ email, password })` |
|
|
524
|
+
| Sign up | `authClient.signUp.email({ email, password, name })` |
|
|
525
|
+
| Sign out | `authClient.signOut()` |
|
|
526
|
+
| Get session | `authClient.getSession()` (client) |
|
|
527
|
+
| Get session | `auth.api.getSession({ headers })` (server) |
|
|
528
|
+
| Update user | `authClient.updateUser({ name })` |
|
|
529
|
+
| Reset password | `authClient.forgetPassword({ email })` → `authClient.resetPassword({ token, password })` |
|
|
530
530
|
|
|
531
531
|
## Do's & Don'ts
|
|
532
532
|
|
|
533
533
|
| ✅ Do | ❌ Don't |
|
|
534
534
|
|-------|----------|
|
|
535
|
-
| `auth.api.getSession()`
|
|
536
|
-
| `authClient`
|
|
537
|
-
|
|
|
538
|
-
| HTTPS
|
|
539
|
-
| `baseURL`
|
|
535
|
+
| Use `auth.api.getSession()` on server | Manipulate session directly on client |
|
|
536
|
+
| Use `authClient` on client | Hardcode secrets |
|
|
537
|
+
| Manage secrets with environment variables | Store session tokens in localStorage |
|
|
538
|
+
| HTTPS required in production | Deploy with HTTP in production |
|
|
539
|
+
| Configure `baseURL` per environment | Hardcode absolute paths |
|
|
540
540
|
|
|
541
541
|
</patterns>
|