@withmata/blueprints 0.2.0

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 (77) hide show
  1. package/.claude/commands/audit.md +179 -0
  2. package/.claude/commands/discover.md +92 -0
  3. package/.claude/commands/new-blueprint.md +265 -0
  4. package/.claude/commands/new-project.md +230 -0
  5. package/.claude/commands/scaffold-auth.md +310 -0
  6. package/.claude/commands/scaffold-db.md +270 -0
  7. package/.claude/commands/scaffold-foundation.md +158 -0
  8. package/.cursor/commands/audit.md +179 -0
  9. package/.cursor/commands/discover.md +92 -0
  10. package/.cursor/commands/new-blueprint.md +265 -0
  11. package/.cursor/commands/new-project.md +230 -0
  12. package/.cursor/commands/scaffold-auth.md +310 -0
  13. package/.cursor/commands/scaffold-db.md +270 -0
  14. package/.cursor/commands/scaffold-foundation.md +158 -0
  15. package/.opencode/commands/audit.md +183 -0
  16. package/.opencode/commands/discover.md +96 -0
  17. package/.opencode/commands/new-blueprint.md +269 -0
  18. package/.opencode/commands/new-project.md +234 -0
  19. package/.opencode/commands/scaffold-auth.md +314 -0
  20. package/.opencode/commands/scaffold-db.md +274 -0
  21. package/.opencode/commands/scaffold-foundation.md +162 -0
  22. package/ENGINEERING.md +47 -0
  23. package/blueprints/discovery/product-discovery/BLUEPRINT.md +361 -0
  24. package/blueprints/discovery/product-discovery/templates/archetype-template.md +143 -0
  25. package/blueprints/discovery/product-discovery/templates/product-brief.md +65 -0
  26. package/blueprints/discovery/product-discovery/templates/product-thesis.md +64 -0
  27. package/blueprints/discovery/product-discovery/templates/research-summary.md +43 -0
  28. package/blueprints/features/auth-better-auth/BLUEPRINT.md +794 -0
  29. package/blueprints/features/auth-better-auth/files/client/auth-client.ts +31 -0
  30. package/blueprints/features/auth-better-auth/files/client/context/organization.ts +236 -0
  31. package/blueprints/features/auth-better-auth/files/client/hooks/use-create-organization.ts +45 -0
  32. package/blueprints/features/auth-better-auth/files/client/hooks/use-has-permission.ts +26 -0
  33. package/blueprints/features/auth-better-auth/files/client/hooks/use-organization.ts +64 -0
  34. package/blueprints/features/auth-better-auth/files/client/schema/auth.ts +21 -0
  35. package/blueprints/features/auth-better-auth/files/client/schema/organization.ts +51 -0
  36. package/blueprints/features/auth-better-auth/files/client/types/organization.ts +20 -0
  37. package/blueprints/features/auth-better-auth/files/db/auth-schema.ts +184 -0
  38. package/blueprints/features/auth-better-auth/files/db/drizzle.config.ts +21 -0
  39. package/blueprints/features/auth-better-auth/files/middleware/route-protection.ts +84 -0
  40. package/blueprints/features/auth-better-auth/files/server/auth-db.ts +18 -0
  41. package/blueprints/features/auth-better-auth/files/server/auth.ts +159 -0
  42. package/blueprints/features/auth-better-auth/files/server/env.ts +44 -0
  43. package/blueprints/features/auth-better-auth/files/server/middleware.ts +144 -0
  44. package/blueprints/features/db-drizzle-postgres/BLUEPRINT.md +596 -0
  45. package/blueprints/features/db-drizzle-postgres/files/db/drizzle.config.ts +18 -0
  46. package/blueprints/features/db-drizzle-postgres/files/db/env.example +3 -0
  47. package/blueprints/features/db-drizzle-postgres/files/db/package.json +33 -0
  48. package/blueprints/features/db-drizzle-postgres/files/db/src/client.ts +34 -0
  49. package/blueprints/features/db-drizzle-postgres/files/db/src/example-entity.ts +73 -0
  50. package/blueprints/features/db-drizzle-postgres/files/db/src/index.ts +8 -0
  51. package/blueprints/features/db-drizzle-postgres/files/db/src/scripts/seed.ts +50 -0
  52. package/blueprints/features/db-drizzle-postgres/files/db/tsconfig.json +13 -0
  53. package/blueprints/features/tailwind-v4/BLUEPRINT.md +29 -0
  54. package/blueprints/features/ui-shared-components/BLUEPRINT.md +35 -0
  55. package/blueprints/foundation/monorepo-turbo/BLUEPRINT.md +378 -0
  56. package/blueprints/foundation/monorepo-turbo/files/apps/web/app/globals.css +2 -0
  57. package/blueprints/foundation/monorepo-turbo/files/apps/web/app/layout.tsx +23 -0
  58. package/blueprints/foundation/monorepo-turbo/files/apps/web/app/page.tsx +7 -0
  59. package/blueprints/foundation/monorepo-turbo/files/apps/web/env.ts +13 -0
  60. package/blueprints/foundation/monorepo-turbo/files/apps/web/next.config.ts +12 -0
  61. package/blueprints/foundation/monorepo-turbo/files/apps/web/package.json +28 -0
  62. package/blueprints/foundation/monorepo-turbo/files/apps/web/postcss.config.mjs +5 -0
  63. package/blueprints/foundation/monorepo-turbo/files/apps/web/tsconfig.json +18 -0
  64. package/blueprints/foundation/monorepo-turbo/files/config/tailwind-config/package.json +14 -0
  65. package/blueprints/foundation/monorepo-turbo/files/config/tailwind-config/postcss.config.mjs +5 -0
  66. package/blueprints/foundation/monorepo-turbo/files/config/tailwind-config/shared-styles.css +88 -0
  67. package/blueprints/foundation/monorepo-turbo/files/config/typescript-config/base.json +19 -0
  68. package/blueprints/foundation/monorepo-turbo/files/config/typescript-config/nextjs.json +12 -0
  69. package/blueprints/foundation/monorepo-turbo/files/config/typescript-config/package.json +5 -0
  70. package/blueprints/foundation/monorepo-turbo/files/config/typescript-config/react-library.json +7 -0
  71. package/blueprints/foundation/monorepo-turbo/files/root/biome.json +46 -0
  72. package/blueprints/foundation/monorepo-turbo/files/root/gitignore +35 -0
  73. package/blueprints/foundation/monorepo-turbo/files/root/package.json +25 -0
  74. package/blueprints/foundation/monorepo-turbo/files/root/pnpm-workspace.yaml +5 -0
  75. package/blueprints/foundation/monorepo-turbo/files/root/turbo.json +30 -0
  76. package/dist/index.js +453 -0
  77. package/package.json +53 -0
@@ -0,0 +1,230 @@
1
+ # New Project
2
+
3
+ Take a product from idea to running code. This command walks through the full lifecycle — defining the product, setting up the codebase to support it, and wiring in the capabilities it needs. You're a product partner with deep engineering expertise: the product strategy drives every technical decision.
4
+
5
+ ## Step 1: Read the Blueprints
6
+
7
+ Load all available blueprints so you understand what's possible:
8
+
9
+ ```
10
+ ~/Documents/WithMata/my-blueprints/blueprints/discovery/product-discovery/BLUEPRINT.md
11
+ ~/Documents/WithMata/my-blueprints/blueprints/discovery/product-discovery/templates/*
12
+ ~/Documents/WithMata/my-blueprints/blueprints/foundation/monorepo-turbo/BLUEPRINT.md
13
+ ~/Documents/WithMata/my-blueprints/blueprints/foundation/monorepo-turbo/files/**/*
14
+ ```
15
+
16
+ Also scan for available feature blueprints:
17
+
18
+ ```
19
+ ~/Documents/WithMata/my-blueprints/blueprints/features/*/BLUEPRINT.md
20
+ ```
21
+
22
+ Only consider a feature blueprint "available" if its BLUEPRINT.md has `Status: Complete` (not a placeholder). If not found at the paths above, ask the user for the path to their blueprints repo.
23
+
24
+ ## Step 2: Assess Current State
25
+
26
+ Before doing anything, understand where this project stands.
27
+
28
+ **Check `.project-context.md`** in the target project root. This file tracks what has already been done:
29
+
30
+ - **Has a `## Product Discovery` section?** Discovery is complete.
31
+ - **Has a `## Foundation` section?** Foundation is scaffolded.
32
+ - **Has entries under `## Installed Blueprints`?** Some features are already installed.
33
+
34
+ **Explore the target directory:**
35
+
36
+ - Empty directory? Fresh start — begin from Stage 1.
37
+ - Has `docs/product/`? Discovery was done (even without a context file).
38
+ - Has `package.json`, `turbo.json`, workspace dirs? Foundation was done.
39
+ - Has auth files, DB packages, etc.? Features were installed.
40
+
41
+ ## Step 3: Determine Starting Point
42
+
43
+ Based on what you found, tell the user where things stand. Lead with the product, not the tech:
44
+
45
+ - **Nothing exists**: "This is a blank slate. Let's start by figuring out what you're building and who it's for — then we'll set up the codebase to support it."
46
+
47
+ - **Discovery done, nothing else**: "You've already defined [product name] — [problem statement in one line]. We've got a clear picture of the product. Let's turn that into something you can ship."
48
+
49
+ - **Discovery + Foundation done**: "The product is defined and the codebase is ready. Based on what [primary archetype] needs, let's wire in the right capabilities."
50
+
51
+ - **Everything done**: "This project looks fully set up. Want to add more capabilities, or revisit any of the product decisions?"
52
+
53
+ If the user wants to start at a specific stage, let them. If they want to redo a completed stage, confirm before proceeding (it may be destructive).
54
+
55
+ ---
56
+
57
+ ## Stage 1: Discovery
58
+
59
+ > "Before we write any code, let's get clear on what we're building and why it matters."
60
+
61
+ This is the most important stage. Everything downstream — the architecture, the features, the trade-offs — flows from what we define here. Don't rush it.
62
+
63
+ Follow the full discovery process from the product-discovery blueprint. This is a rigorous product conversation — challenge assumptions, research the market, and push until the product thesis is sharp. You'll work through:
64
+
65
+ 1. **Initial Framing** — What are you building? Who is it for? Why does it need to exist?
66
+ 2. **Problem Deep Dive** — 5 Whys to get past symptoms to root cause
67
+ 3. **User Definition** — Vivid archetypes and personas via scenario mapping + JTBD
68
+ 4. **Solution Positioning** — Competitive landscape, differentiation, "why now?"
69
+ 5. **Synthesis** — Distill everything into a defensible product thesis
70
+
71
+ Use the templates from the blueprint to generate:
72
+
73
+ ```
74
+ docs/product/
75
+ ├── product-thesis.md # The 2-minute read — pillars, metrics, positioning
76
+ ├── product-brief.md # Supporting research and detail
77
+ └── users/
78
+ ├── archetype-[name].md # Vivid portraits of who you're building for
79
+ └── research-summary.md # Evidence and sources
80
+ ```
81
+
82
+ Create or update `.project-context.md` with the `## Product Discovery` section.
83
+
84
+ ### Pause Point
85
+
86
+ After discovery is complete, connect what was defined to what comes next:
87
+
88
+ "We've defined [product name] around [N] core pillars: [list them]. The primary user is [archetype] — [one-line description of their world]. This gives us a clear foundation to build on. Want to set up the codebase now, or let this sit and come back to it?"
89
+
90
+ If stopping: "Whenever you're ready, run `/scaffold-foundation` to set up the project, or `/new-project` again to pick up where we left off."
91
+
92
+ ---
93
+
94
+ ## Stage 1.5: Project Type
95
+
96
+ After discovery is complete, determine the project type:
97
+
98
+ "Now let's set up the codebase. Are you building a:
99
+ 1. **Monorepo** — multiple packages, shared config, Turborepo (recommended for larger products with separate frontend/backend/packages)
100
+ 2. **Single-repo** — one application, everything in one package.json (simpler for smaller products or standalone apps)"
101
+
102
+ Record the choice in `.project-context.md` under `## Foundation`:
103
+ ```yaml
104
+ project_type: monorepo # or single-repo
105
+ ```
106
+
107
+ - If **monorepo**: proceed to Stage 2 (Foundation)
108
+ - If **single-repo**: skip Stage 2 entirely, proceed to Stage 3 (Features). Note in context: "Foundation skipped — single-repo project."
109
+
110
+ ---
111
+
112
+ ## Stage 2: Foundation
113
+
114
+ > "The product is defined. Let's build the structure to support it."
115
+
116
+ > **Note:** Skip this stage for single-repo projects — proceed directly to Stage 3.
117
+
118
+ Read the discovery context from `.project-context.md` to inform technical decisions:
119
+
120
+ - Derive the npm scope from the product name (e.g., "Acme Dashboard" -> `@acme`)
121
+ - Use the product pillars and archetype needs to inform any trade-offs
122
+ - Apply technical constraints from discovery if any were noted
123
+
124
+ Follow the foundation blueprint process. Only three questions — skip any already answered by project context:
125
+
126
+ 1. **npm scope** — defaults from product name
127
+ 2. **Project name** — human-readable, for metadata and page titles
128
+ 3. **Node.js version** — defaults to `>=22`
129
+
130
+ Scaffold the monorepo skeleton: root configs, TypeScript config package, Tailwind config package, Next.js app shell, and empty workspace directories for future blueprints.
131
+
132
+ After scaffolding:
133
+
134
+ 1. Run `pnpm install` to verify the workspace is valid
135
+ 2. Briefly verify the dev server boots: `pnpm dev`
136
+ 3. Append the `## Foundation` section to `.project-context.md`
137
+
138
+ ### Pause Point
139
+
140
+ After foundation is complete, bridge to features by referencing the product:
141
+
142
+ "The codebase is ready — [scope] monorepo with Turborepo, Biome, and a Next.js app shell. Now let's think about what [product name] needs to actually work. Want to look at what's available, or stop here for now?"
143
+
144
+ If stopping: "You can add capabilities anytime. Run `/scaffold-auth` for authentication, or `/new-project` to see all available blueprints."
145
+
146
+ ---
147
+
148
+ ## Stage 3: Features
149
+
150
+ > "Let's wire in what [product name] needs to deliver on its core pillars."
151
+
152
+ If discovery was completed, reference the product pillars and archetype needs when presenting features. Connect each blueprint back to the product — don't just list technical capabilities.
153
+
154
+ List the available feature blueprints. Read each `BLUEPRINT.md` under `blueprints/features/*/` and show only those with `Status: Complete`.
155
+
156
+ **Present features in dependency order** — blueprints that others depend on should be listed first. Check each blueprint's `## Blueprint Dependencies` section. For example, db should be listed before auth (since auth requires db).
157
+
158
+ For each available blueprint, show:
159
+ - **Name** — human-readable
160
+ - **What it enables for the product** — tie it to a pillar or user need when possible
161
+ - **Dependencies** — note if it requires another blueprint (e.g., "requires Database")
162
+ - **Command** — the slash command to run it individually
163
+
164
+ Example:
165
+
166
+ ```
167
+ Based on what [product name] needs:
168
+
169
+ 1. Authentication (Better Auth + Drizzle) — /scaffold-auth
170
+ User accounts, login flows, route protection. Required for [relevant pillar].
171
+ If org plugin: team/workspace management for [archetype need].
172
+ ```
173
+
174
+ Ask: "Which of these does [product name] need? We'll set them up one at a time."
175
+
176
+ If no discovery context exists, fall back to a straightforward capability list without the product framing.
177
+
178
+ ### For Each Selected Feature
179
+
180
+ 1. Read the feature's BLUEPRINT.md and all its files
181
+ 2. Read `.project-context.md` to understand current state
182
+ 3. Follow the blueprint's scaffolding process (ask its configuration questions, adapt files, etc.)
183
+ 4. Append the feature's entry to `## Installed Blueprints` in .project-context.md
184
+
185
+ After each feature is installed, ask: "[Feature] is wired in. Want to add another, or is [product name] ready to start building on?"
186
+
187
+ ---
188
+
189
+ ## Step 7: Final Summary
190
+
191
+ Once the user is done (whether after one stage or all three), provide a summary that connects the product to the technical foundation:
192
+
193
+ ### What We Built
194
+
195
+ - **The product** (if discovery ran) — what it is, who it's for, the core pillars it delivers on
196
+ - **The codebase** (if foundation ran) — how the project structure supports the product
197
+ - **The capabilities** (if features ran) — what's wired in and how it maps to product needs
198
+
199
+ ### Getting Started
200
+
201
+ ```
202
+ pnpm dev # Start the development server
203
+ ```
204
+
205
+ ### Environment Variables to Set
206
+
207
+ List any env vars that need values (aggregated from all stages).
208
+
209
+ ### Manual Steps Remaining
210
+
211
+ List anything that can't be automated (aggregated from all stages).
212
+
213
+ ### What's Next
214
+
215
+ Suggest next steps in terms of product progress, not just technical tasks. Reference specific commands:
216
+
217
+ - "The product thesis identified [pillar] as a core pillar — `/scaffold-auth` wires in the user accounts and access control to support that."
218
+ - "Other feature blueprints coming soon: UI components, database, Tailwind theming."
219
+ - "Start building the core product experience — the [archetype]'s primary workflow."
220
+
221
+ ---
222
+
223
+ ## Important Notes
224
+
225
+ - **Resumable** — this command always checks `.project-context.md` before starting. Run it again anytime to pick up where you left off, or use the individual stage commands (`/discover`, `/scaffold-foundation`, `/scaffold-auth`).
226
+ - **Non-destructive** — never overwrite existing files without asking. If a stage was already completed, confirm before redoing it.
227
+ - **Product-first** — every technical decision should trace back to the product definition. When in doubt, reference the pillars, the archetype, and the problem statement.
228
+ - **Conversational** — this is a guided conversation, not a script. Adapt to the user's pace. If they want to skip ahead, let them. If they want to dig deeper into a stage, follow their lead.
229
+ - **Project context is the source of truth** — every stage reads it, every stage appends to it. This is how stages communicate with each other across sessions.
230
+ - **Maintenance rules accumulate** — each stage injects its maintenance rules into the project's rules file(s). These rules tell the AI coding tool what actions to take after certain types of changes. They accumulate as more blueprints are installed — the monorepo rules from foundation, plus auth rules, plus database rules, etc. The tool reads these at the start of every session.
@@ -0,0 +1,310 @@
1
+ # Scaffold Auth Blueprint
2
+
3
+ Scaffold the `auth-better-auth` blueprint into the current project. Read the full blueprint first, then adapt it to this project's stack.
4
+
5
+ ## Step 1: Read the Blueprint
6
+
7
+ Read the full BLUEPRINT.md and all code files from the auth blueprint:
8
+
9
+ ```
10
+ blueprints/features/auth-better-auth/BLUEPRINT.md
11
+ blueprints/features/auth-better-auth/files/**/*
12
+ ```
13
+
14
+ If these files are not in the current project, look for them at `~/Documents/WithMata/my-blueprints/blueprints/features/auth-better-auth/`. If still not found, ask the user for the path to their blueprints repo.
15
+
16
+ ## Step 1.5: Read Project Context
17
+
18
+ Check if `.project-context.md` exists in the target project root:
19
+
20
+ - **If it exists**: read it to understand:
21
+ - Product name and scope (from Discovery)
22
+ - npm scope, workspace layout, server framework (from Foundation)
23
+ - What other blueprints are already installed (from Installed Blueprints)
24
+ - Use this information to pre-fill configuration questions in Step 3 and adapt file paths in Step 4
25
+
26
+ - **If it does not exist**: proceed normally — explore the project manually (Step 2) and ask all configuration questions (Step 3)
27
+
28
+ When project context provides clear answers, skip the corresponding questions in Step 3. For example:
29
+ - Context says `server_framework: hono` -> do not ask about server framework
30
+ - Context says `npm_scope: "@acme"` -> use `@acme` for package references
31
+ - Context has `auth-better-auth` in Installed Blueprints -> warn about existing install
32
+
33
+ ## Step 1.7: Check Blueprint Dependencies
34
+
35
+ Read the Blueprint Dependencies section from `auth-better-auth/BLUEPRINT.md`.
36
+
37
+ For `features/db-drizzle-postgres` (required):
38
+ - Check `.project-context.md` for `db-drizzle-postgres` under `## Installed Blueprints`
39
+ - If installed: proceed normally
40
+ - If NOT installed, check filesystem:
41
+ - Monorepo: does `packages/db/` exist?
42
+ - Single-repo: does `src/db/` exist?
43
+ - If db directory exists (but not in project context): note it, proceed normally — the scaffold command already handles merging
44
+ - If neither installed nor directory exists:
45
+ - Ask: "Auth requires a database package. The db-drizzle-postgres blueprint establishes the schema-group pattern that auth builds on. Install it first? (Y/n)"
46
+ - If yes: run `/scaffold-db` first, then continue with auth
47
+ - If no: warn about missing patterns, proceed with creating a minimal db structure for auth only
48
+
49
+ For `foundation/monorepo-turbo` (recommended):
50
+ - If not present: mention once: "Tip: The monorepo-turbo foundation blueprint provides workspace structure that auth integrates with. Consider running `/scaffold-foundation` first."
51
+ - Continue without blocking.
52
+
53
+ ## Step 2: Understand the Target Project
54
+
55
+ Before making any changes, explore the target project to understand:
56
+
57
+ ### 2a. Detect Project Type
58
+
59
+ Check `.project-context.md` for `project_type`. If not present, detect:
60
+
61
+ - **Monorepo indicators**: `pnpm-workspace.yaml`, `turbo.json`, `nx.json`, `lerna.json`, or `"workspaces"` in root `package.json`
62
+ - **Single-repo indicators**: Single `package.json` at root, `src/` directory, no workspace config
63
+
64
+ Confirm with user: "This looks like a [monorepo|single-repo] project. Is that right?"
65
+
66
+ Record the project type in `.project-context.md` if not already there.
67
+
68
+ ### 2b. Explore Structure
69
+
70
+ **If monorepo:**
71
+ 1. What workspaces exist? Where do apps, APIs, and packages live?
72
+ 2. Is there a Hono server, Express server, or is it Next.js-only? Where are API routes?
73
+ 3. Is Drizzle already set up? Is there a shared DB package?
74
+ 4. Is this Next.js? What version? Is there an existing middleware.ts?
75
+
76
+ **If single-repo:**
77
+ 1. What framework is this? (Next.js, Node/Express, Hono, Bun?)
78
+ 2. Does `src/db/` or `lib/db/` exist?
79
+ 3. What import convention is used? (`@/` paths, `#` subpath imports, relative?)
80
+ 4. Is there an existing `middleware.ts`?
81
+
82
+ **Both:**
83
+ 5. Existing auth to replace or integrate with?
84
+ 6. UI library? Styling approach?
85
+ 7. Package manager?
86
+ 8. Validation — Zod, t3-env?
87
+ 9. Data fetching — SWR, TanStack Query?
88
+
89
+ Skip exploration for anything already documented in `.project-context.md`.
90
+
91
+ ## Step 3: Ask Clarifying Questions
92
+
93
+ Before scaffolding, ask the user about their preferences. Use the blueprint's "Hard Requirements vs Recommended Defaults" table. Specifically ask:
94
+
95
+ 1. **Organization plugin** — "Do you need multi-tenant organizations (teams/workspaces with member management and invitations), or is this a single-tenant app?"
96
+
97
+ 2. **Database setup** — "Should auth use a separate database, or share the existing app database?" (Recommend separate if no strong preference.)
98
+
99
+ 3. **Social providers** — "Which social login providers do you want? Google is included by default. Any others (GitHub, Discord, etc.)?"
100
+
101
+ 4. **Email provider** — "The blueprint uses Resend for transactional emails (verification, password reset, invitations). Do you want to use Resend, or a different provider?"
102
+
103
+ 5. **Defaults check** — "The blueprint recommends these tools (which may already match your project). Want to go with the defaults, or change any?"
104
+ - Zod for validation
105
+ - t3-env for environment variable validation
106
+ - SWR (`useSWRMutation`) for auth mutation hooks
107
+ - react-hook-form for auth forms
108
+
109
+ Skip questions where the answer is obvious from the project structure or from `.project-context.md`.
110
+
111
+ ## Step 4: Adapt and Scaffold
112
+
113
+ Based on the project structure, project context, and user answers, adapt the blueprint files:
114
+
115
+ ### 4a. Database Layer
116
+
117
+ **If monorepo:**
118
+ - Copy/adapt `files/db/drizzle.config.ts` → `packages/db/drizzle/auth/drizzle.config.ts`
119
+ - Copy `files/db/auth-schema.ts` as a reference (the actual schema will be auto-generated)
120
+ - Add the `"./auth"` export to `packages/db/package.json`
121
+ - Add `auth:generate` and `auth:migrate` scripts to `packages/db/package.json`
122
+
123
+ **If single-repo:**
124
+ - Copy/adapt `files/db/drizzle.config.ts` → `drizzle/auth/drizzle.config.ts` (at project root)
125
+ - Adjust schema paths to `"./src/db/auth/schema.ts"` (relative to root)
126
+ - Create `src/db/auth/` directory for auth schema
127
+ - No separate package.json export needed — use direct imports
128
+
129
+ ### 4b. Server Layer
130
+
131
+ **If monorepo:**
132
+ - Copy/adapt `files/server/auth.ts` → `apis/server/src/auth/index.ts`
133
+ - Replace `{{APP_NAME}}`, `{{EMAIL_FROM}}`, `{{SCOPE}}`
134
+ - Copy/adapt `files/server/auth-db.ts` → `apis/server/src/db/auth.ts`
135
+ - Copy/adapt `files/server/middleware.ts` → `apis/server/src/middleware/auth.ts`
136
+ - Merge auth env vars into `apis/server/src/env.ts`
137
+
138
+ **If single-repo:**
139
+ - Copy/adapt `files/server/auth.ts` → `src/lib/auth/index.ts`
140
+ - Replace `{{APP_NAME}}`, `{{EMAIL_FROM}}`
141
+ - Adjust DB import to use direct path: `import { db } from "@/db/auth/client"` or `#db/auth/client`
142
+ - Copy/adapt `files/server/auth-db.ts` → `src/lib/db/auth.ts`
143
+ - Default to Next.js API routes (no separate Hono server)
144
+ - Merge auth env vars into existing `env.ts` (or create one)
145
+
146
+ **Both:**
147
+ - If the project uses a different server framework than Hono, adapt the middleware while keeping the core `auth.api.getSession({ headers })` pattern
148
+
149
+ ### 4c. Mount Auth Handler
150
+
151
+ **If monorepo with Hono server:**
152
+ - Add CORS config + `app.on(["POST", "GET"], "/api/auth/*", ...)` handler in `apis/server/`
153
+
154
+ **If single-repo or Next.js API routes:**
155
+ - Create `app/api/auth/[...all]/route.ts` with `toNextJsHandler(auth)`
156
+ - No CORS config needed (same origin)
157
+
158
+ **Other frameworks:** Follow the platform integration section in BLUEPRINT.md
159
+
160
+ ### 4d. Client Layer
161
+
162
+ **If monorepo:**
163
+ - Copy client files to `apps/web/auth/`
164
+ - Add `"#auth/*": "./auth/*"` import alias to `apps/web/package.json`
165
+
166
+ **If single-repo:**
167
+ - Copy client files to `src/auth/`
168
+ - Add `"#auth/*": "./src/auth/*"` import alias to root `package.json` (or use `@/auth/*` if that's the convention)
169
+
170
+ **Both:**
171
+ - Adapt `files/client/auth-client.ts` — adjust baseURL for deployment setup
172
+ - If using org plugin: copy all `files/client/context/`, `hooks/`, `schema/`, `types/` files
173
+ - If NOT using org plugin: only copy `files/client/schema/auth.ts`
174
+
175
+ ### 4e. Route Protection
176
+
177
+ - Copy/adapt `files/middleware/route-protection.ts` into the project's Next.js middleware
178
+ - If middleware.ts already exists, MERGE the auth logic into it — don't overwrite
179
+ - If using Hono standalone mode: add API proxy rewrites to `next.config.ts`
180
+
181
+ ### 4f. Project Scripts
182
+
183
+ **If monorepo:**
184
+ - Add `auth:generate` script to the server package (runs `@better-auth/cli generate`)
185
+ - Add `auth:generate`, `auth:migrate`, `auth:setup` to root `package.json` (Turbo wrappers)
186
+ - Add `auth:generate` and `auth:migrate` tasks to `turbo.json` (both `cache: false`)
187
+
188
+ **If single-repo:**
189
+ - Add scripts directly to root `package.json`:
190
+ - `auth:generate` -> `npx @better-auth/cli@latest generate --config ./src/lib/auth/index.ts --output ./src/db/auth/schema.ts --yes`
191
+ - `auth:migrate` -> `drizzle-kit generate --config ./drizzle/auth/drizzle.config.ts && drizzle-kit migrate --config ./drizzle/auth/drizzle.config.ts`
192
+ - `auth:setup` -> `pnpm auth:generate && pnpm auth:migrate`
193
+ - No Turbo tasks needed
194
+
195
+ ## Step 5: Update Project Context
196
+
197
+ Append an entry to `.project-context.md` under `## Installed Blueprints`:
198
+
199
+ ```yaml
200
+ ### auth-better-auth
201
+ blueprint: auth-better-auth
202
+ choices:
203
+ database: <user's choice>
204
+ auth_db_isolation: <separate|shared>
205
+ server_framework: <hono|next.js>
206
+ organization_plugin: <true|false>
207
+ social_providers: [<list>]
208
+ email_provider: <resend|other>
209
+ env_validation: <t3-env|other>
210
+ form_validation: <zod|other>
211
+ data_fetching: <swr|other>
212
+ files_created:
213
+ - <list of all files created or modified>
214
+ env_vars_added:
215
+ - <list of env vars>
216
+ scripts_added:
217
+ - <list of scripts>
218
+ ```
219
+
220
+ If `.project-context.md` does not exist, create it with the standard header and the Installed Blueprints section. Log a note: "No discovery or foundation context found. Consider running `/discover` and `/scaffold-foundation` for a complete project context."
221
+
222
+ Also append any significant architectural decisions to the `## Architecture Decisions` section.
223
+
224
+ ### Inject Maintenance Rules
225
+
226
+ Append the condensed maintenance rules to the target project's rules file(s):
227
+
228
+ 1. If `CLAUDE.md` exists in the target project → append rules there
229
+ 2. If `AGENTS.md` exists in the target project → append rules there
230
+ 3. If both exist → append to both (keep them in sync)
231
+ 4. If neither exists → ask the user which AI coding tool(s) they use:
232
+ - Claude Code → create CLAUDE.md with a `## Maintenance Rules` section
233
+ - OpenCode or Cursor → create AGENTS.md with a `## Maintenance Rules` section (both tools read AGENTS.md)
234
+ - Multiple tools → create all applicable files
235
+
236
+ The maintenance rules content is identical regardless of which file it goes into:
237
+
238
+ ```markdown
239
+ ### auth-better-auth maintenance
240
+ - After modifying Better Auth server config, plugins, or providers: run `pnpm auth:generate && pnpm auth:migrate`
241
+ - After updating `better-auth` package: run `pnpm auth:generate`, check for schema changes, migrate if needed
242
+ - After adding/changing env vars: update all `.env` files and verify `pnpm dev` starts clean
243
+ - After changing `BETTER_AUTH_URL` or `FRONTEND_DOMAIN`: verify CORS config and OAuth callback URLs
244
+ - After any auth changes: verify `{BETTER_AUTH_URL}/api/auth/ok` responds
245
+ - Never edit the auto-generated auth schema directly — modify the Better Auth config and regenerate
246
+ ```
247
+
248
+ If the `## Maintenance Rules` heading already exists (from another blueprint), only add the `### auth-better-auth maintenance` subsection — do not duplicate the heading.
249
+
250
+ ## Step 6: Output Summary
251
+
252
+ After scaffolding, provide the user with a clear summary:
253
+
254
+ ### Packages to Install
255
+
256
+ List every package that needs to be installed, grouped by workspace:
257
+
258
+ ```
259
+ # Server (apis/server or wherever auth lives)
260
+ pnpm add better-auth drizzle-orm pg resend @t3-oss/env-core zod dotenv
261
+
262
+ # Client (apps/web)
263
+ pnpm add better-auth swr react-hook-form @hookform/resolvers zod @t3-oss/env-nextjs
264
+
265
+ # DB package (packages/db) — dev deps
266
+ pnpm add -D drizzle-kit pg
267
+ ```
268
+
269
+ Adjust based on what's already installed in the project.
270
+
271
+ ### Environment Variables
272
+
273
+ List every env var that needs to be set, with example values:
274
+
275
+ ```env
276
+ # Server
277
+ BETTER_AUTH_URL=http://localhost:4000
278
+ BETTER_AUTH_SECRET= # Generate: openssl rand -base64 32
279
+ AUTH_DATABASE_URL= # PostgreSQL connection URL
280
+ FRONTEND_DOMAIN=http://localhost:3000
281
+ GOOGLE_CLIENT_ID= # From Google Cloud Console
282
+ GOOGLE_CLIENT_SECRET= # Redirect URI: {BETTER_AUTH_URL}/api/auth/callback/google
283
+ RESEND_API_KEY= # From resend.com
284
+
285
+ # Client (.env.local in web app)
286
+ NEXT_PUBLIC_API_URL=http://localhost:4000
287
+ NEXT_PUBLIC_FRONTEND_URL=http://localhost:3000
288
+ ```
289
+
290
+ ### Manual Steps Remaining
291
+
292
+ List anything that can't be automated:
293
+
294
+ 1. **Create a PostgreSQL database** for auth (if using separate auth DB)
295
+ 2. **Set up Google OAuth** — create OAuth 2.0 credentials in Google Cloud Console, add redirect URI
296
+ 3. **Set up Resend** — create an account, verify your domain, get API key
297
+ 4. **Run auth setup** — `pnpm auth:setup` (generates schema + runs migrations)
298
+ 5. **Build auth UI pages** — follow the Auth Flows section in BLUEPRINT.md to create the pages matching your design system
299
+ 6. **Test the auth flow** — sign up, verify email, sign in, create org (if applicable)
300
+
301
+ ### Files Created
302
+
303
+ List every file that was created or modified, with a one-line description.
304
+
305
+ ## Important Notes
306
+
307
+ - If the Better Auth skills are installed (`npx skillsadd better-auth/skills`), invoke `/best-practices` for quick-reference on configuration. If not available, refer to the Better Auth docs and References section in BLUEPRINT.md.
308
+ - When adapting code, preserve the `// CONFIGURE:` comments so the user can find customization points later.
309
+ - If the project structure differs significantly from the blueprint's assumptions, explain the differences and suggest how to adapt rather than forcing the blueprint's structure.
310
+ - Never overwrite existing files without asking. If a file exists (like `middleware.ts` or `env.ts`), merge the auth additions into it.