nextworks 0.1.0-alpha.11 → 0.1.0-alpha.14

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 (57) hide show
  1. package/README.md +20 -9
  2. package/dist/kits/auth-core/.nextworks/docs/AUTH_CORE_README.md +3 -3
  3. package/dist/kits/auth-core/.nextworks/docs/AUTH_QUICKSTART.md +264 -244
  4. package/dist/kits/auth-core/app/(protected)/settings/profile/profile-form.tsx +120 -114
  5. package/dist/kits/auth-core/app/api/auth/forgot-password/route.ts +116 -114
  6. package/dist/kits/auth-core/app/api/auth/reset-password/route.ts +66 -63
  7. package/dist/kits/auth-core/app/api/auth/send-verify-email/route.ts +1 -1
  8. package/dist/kits/auth-core/app/api/users/[id]/route.ts +134 -127
  9. package/dist/kits/auth-core/app/auth/reset-password/page.tsx +186 -187
  10. package/dist/kits/auth-core/components/auth/dashboard.tsx +25 -2
  11. package/dist/kits/auth-core/components/auth/forgot-password-form.tsx +90 -90
  12. package/dist/kits/auth-core/components/auth/login-form.tsx +492 -467
  13. package/dist/kits/auth-core/components/auth/signup-form.tsx +28 -29
  14. package/dist/kits/auth-core/lib/auth.ts +46 -15
  15. package/dist/kits/auth-core/lib/forms/map-errors.ts +37 -11
  16. package/dist/kits/auth-core/lib/server/result.ts +45 -45
  17. package/dist/kits/auth-core/lib/validation/forms.ts +1 -2
  18. package/dist/kits/auth-core/package-deps.json +4 -2
  19. package/dist/kits/auth-core/types/next-auth.d.ts +1 -1
  20. package/dist/kits/blocks/.nextworks/docs/BLOCKS_QUICKSTART.md +2 -8
  21. package/dist/kits/blocks/.nextworks/docs/THEME_GUIDE.md +18 -1
  22. package/dist/kits/blocks/app/templates/productlaunch/page.tsx +0 -2
  23. package/dist/kits/blocks/components/sections/FAQ.tsx +0 -1
  24. package/dist/kits/blocks/components/sections/Newsletter.tsx +2 -2
  25. package/dist/kits/blocks/components/ui/switch.tsx +78 -78
  26. package/dist/kits/blocks/components/ui/theme-selector.tsx +1 -1
  27. package/dist/kits/blocks/lib/themes.ts +1 -0
  28. package/dist/kits/blocks/package-deps.json +4 -4
  29. package/dist/kits/data/.nextworks/docs/DATA_QUICKSTART.md +128 -112
  30. package/dist/kits/data/.nextworks/docs/DATA_README.md +2 -1
  31. package/dist/kits/data/app/api/posts/[id]/route.ts +83 -83
  32. package/dist/kits/data/app/api/posts/route.ts +136 -138
  33. package/dist/kits/data/app/api/seed-demo/route.ts +1 -2
  34. package/dist/kits/data/app/api/users/[id]/route.ts +29 -17
  35. package/dist/kits/data/app/api/users/check-email/route.ts +1 -1
  36. package/dist/kits/data/app/api/users/check-unique/route.ts +30 -27
  37. package/dist/kits/data/app/api/users/route.ts +0 -2
  38. package/dist/kits/data/app/examples/demo/create-post-form.tsx +108 -106
  39. package/dist/kits/data/app/examples/demo/page.tsx +2 -1
  40. package/dist/kits/data/app/examples/demo/seed-demo-button.tsx +1 -1
  41. package/dist/kits/data/components/admin/posts-manager.tsx +727 -719
  42. package/dist/kits/data/components/admin/users-manager.tsx +435 -432
  43. package/dist/kits/data/lib/server/result.ts +5 -2
  44. package/dist/kits/data/package-deps.json +1 -1
  45. package/dist/kits/data/scripts/seed-demo.mjs +1 -2
  46. package/dist/kits/forms/app/api/wizard/route.ts +76 -71
  47. package/dist/kits/forms/app/examples/forms/server-action/page.tsx +78 -71
  48. package/dist/kits/forms/components/hooks/useCheckUnique.ts +85 -79
  49. package/dist/kits/forms/components/ui/form/form-control.tsx +28 -28
  50. package/dist/kits/forms/components/ui/form/form-description.tsx +23 -22
  51. package/dist/kits/forms/components/ui/form/form-item.tsx +21 -21
  52. package/dist/kits/forms/components/ui/form/form-label.tsx +24 -24
  53. package/dist/kits/forms/components/ui/form/form-message.tsx +28 -29
  54. package/dist/kits/forms/components/ui/switch.tsx +78 -78
  55. package/dist/kits/forms/lib/forms/map-errors.ts +1 -1
  56. package/dist/kits/forms/lib/validation/forms.ts +1 -2
  57. package/package.json +1 -1
package/README.md CHANGED
@@ -9,13 +9,24 @@ Nextworks is a CLI that installs **modular Next.js building blocks** into your a
9
9
 
10
10
  > **Status:** early‑access alpha. Expect rough edges and breaking changes between alpha releases.
11
11
  >
12
- > In this alpha, the most reliable setup is to install **Blocks** first using:
12
+ > In this alpha, the most reliable setup is:
13
13
  >
14
- > ```bash
15
- > npx nextworks add blocks --sections --templates
16
- > ```
14
+ > 1. Install the CLI (as a dev dependency) and install dependencies:
15
+ >
16
+ > ```bash
17
+ > npm install -D nextworks
18
+ > npm install
19
+ > ```
20
+ >
21
+ > 2. Install **Blocks** first:
22
+ >
23
+ > ```bash
24
+ > npx nextworks add blocks --sections --templates
25
+ > ```
26
+ >
27
+ > 3. Then add **Auth Core**, **Forms**, and **Data** on top.
17
28
  >
18
- > and then add **Auth Core**, **Forms**, and **Data** on top. Partial setups (e.g. Auth/Data without Blocks) may require manual tweaks and are not yet fully supported.
29
+ > Partial setups (e.g. Auth/Data without Blocks) may require manual tweaks and are not yet fully supported.
19
30
 
20
31
  ---
21
32
 
@@ -112,10 +123,10 @@ Then:
112
123
  NEXTAUTH_SECRET=your-strong-secret
113
124
  ```
114
125
 
115
- 2. Install Prisma (if you haven’t already):
126
+ 2. Install dependencies:
116
127
 
117
128
  ```bash
118
- npm install @prisma/client prisma
129
+ npm install
119
130
  ```
120
131
 
121
132
  3. Run Prisma:
@@ -218,9 +229,9 @@ You can add a short “Nextworks setup” section to your app README:
218
229
 
219
230
  Set DATABASE_URL, NEXTAUTH_URL, NEXTAUTH_SECRET (and any OAuth or email provider vars you need).
220
231
 
221
- 3. Install Prisma (if not present) and run migrations:
232
+ 3. Install dependencies and run migrations:
222
233
 
223
- npm install @prisma/client prisma
234
+ npm install
224
235
  npx prisma generate
225
236
  npx prisma migrate dev -n init
226
237
 
@@ -56,11 +56,11 @@ If you choose a different database provider, update the `provider` field in your
56
56
 
57
57
  Environment variables
58
58
 
59
- The kit relies on the following environment variables. Document these in post-install output or in your project's README:
59
+ The kit relies on the following environment variables. Document these in post-install output or in your project's README.
60
60
 
61
61
  Required (for core auth):
62
62
 
63
- - DATABASE_URL
63
+ - DATABASE_URL (required by Prisma)
64
64
  - NEXTAUTH_URL (e.g. http://localhost:3000)
65
65
  - NEXTAUTH_SECRET
66
66
 
@@ -109,7 +109,7 @@ Post-install steps (recommended)
109
109
  Notes & security
110
110
 
111
111
  - Password reset is intentionally opt-in. Do not enable NEXTWORKS_ENABLE_PASSWORD_RESET in production unless you have configured a real mail provider and replaced the in-memory rate limiter.
112
- - NEXTWORKS_USE_DEV_EMAIL=1 uses Ethereal (nodemailer) and prints a preview URL to server logs for development testing.
112
+ - NEXTWORKS_USE_DEV_EMAIL=1 uses Ethereal (nodemailer) and prints a preview URL to server logs for development testing. **This is intended for `next dev` only**. In production (`next start` / `NODE_ENV=production`), password reset requires a real mail provider (SMTP); otherwise `/api/auth/forgot-password` returns **404 by design**.
113
113
  - Email verification on signup is **not yet implemented**. Stub endpoints and UI are in place at `/api/auth/send-verify-email` and `/auth/verify-email`. The Prisma `VerificationToken` model is already included so a future implementation can be added as a non-breaking enhancement.
114
114
 
115
115
  CLI behavior (for maintainers)
@@ -1,244 +1,264 @@
1
- # Auth Quickstart
2
-
3
- Follow these steps to get email/password auth (and optional GitHub OAuth) running in under 5 minutes.
4
-
5
- If you are using the `nextworks` CLI in your own app, the recommended alpha setup is to install Blocks with sections and templates first, then Auth Core:
6
-
7
- ```bash
8
- npx nextworks add blocks --sections --templates
9
- npx nextworks add auth-core
10
- ```
11
-
12
- Then follow the steps below inside your app.
13
-
14
- ## 1) Copy environment variables
15
-
16
- Create a `.env` file based on `.env.example`:
17
-
18
- ```
19
- cp .env.example .env
20
- ```
21
-
22
- Fill in values for the following environment variables used by the Auth kit:
23
-
24
- - DATABASE_URL — PostgreSQL connection string
25
- - NEXTAUTH_URL — URL of your app (e.g. http://localhost:3000 in dev)
26
- - NEXTAUTH_SECRET — a strong random string (used to sign NextAuth tokens)
27
-
28
- Optional / password reset / email provider vars:
29
-
30
- - GITHUB_ID — (optional) GitHub OAuth client id
31
- - GITHUB_SECRET — (optional) GitHub OAuth client secret
32
- - NEXTWORKS_ENABLE_PASSWORD_RESET — set to `1` to enable the dev password reset scaffold (default: disabled)
33
- - NEXTWORKS_USE_DEV_EMAIL — set to `1` to enable Ethereal dev email transport (for local testing only)
34
- - SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASS, NOREPLY_EMAIL — when using a real SMTP provider (required in production if enable password reset)
35
- - NODE_ENV — (production/dev) used to guard password-reset behavior
36
-
37
- Notes:
38
-
39
- - The Auth kit will only enable the GitHub provider when both GITHUB_ID and GITHUB_SECRET are present.
40
- - Password reset remains disabled by default; enable cautiously and only after configuring a real mail provider in production.
41
-
42
- ## 2) Install and generate Prisma
43
-
44
- If you are using the monorepo directly:
45
-
46
- ```bash
47
- npm install
48
- npx prisma generate
49
- npx prisma migrate dev -n init
50
- ```
51
-
52
- If you installed Auth via the CLI (`npx nextworks add auth-core`), the schema and scripts are already in place in your app — you still need to run:
53
-
54
- ```bash
55
- npm install @prisma/client prisma
56
- npx prisma generate
57
- npx prisma migrate dev -n init
58
- ```
59
-
60
- This applies the Prisma schema and generates the Prisma client.
61
-
62
- ## 3) Start the dev server
63
-
64
- ```
65
- npm run dev
66
- ```
67
-
68
- Visit:
69
-
70
- - http://localhost:3000/auth/signup to create a user
71
- - http://localhost:3000/auth/login to sign in
72
- - http://localhost:3000/dashboard after login
73
-
74
- ## 4) Optional: GitHub OAuth
75
-
76
- If you provided GITHUB_ID and GITHUB_SECRET in `.env`, the GitHub provider will be enabled and the GitHub button will appear on the forms. If not provided, the button will be hidden and only email/password is available.
77
-
78
- ## 5) Roles (optional)
79
-
80
- The `User` model has a `role` field (default: `user`). Role is propagated to the JWT and session so you can gate pages/components. You can later add admin tooling or seed scripts as needed.
81
-
82
- ## 6) Seed and promote admin scripts
83
-
84
- - `scripts/seed-demo.mjs` (already in the repo) creates a demo admin user and sample posts for quick demos.
85
- - `scripts/promote-admin.mjs` (new) is idempotent and promotes an existing user to `admin`:
86
-
87
- ```
88
- node ./scripts/promote-admin.mjs admin@example.com
89
- ```
90
-
91
- ## 7) Forgot password (development scaffold)
92
-
93
- This is a development scaffold that is disabled by default. To enable the password reset feature, set:
94
-
95
- ```
96
- NEXTWORKS_ENABLE_PASSWORD_RESET=1
97
- ```
98
-
99
- - POST `/api/auth/forgot-password` accepts `{ email }`. When enabled the endpoint generates a one-time token stored in the database and (in development) prints a reset link to the server console. The endpoint always returns a generic success message to avoid user enumeration.
100
- - GET `/api/auth/reset-password?token=...` validates a token (only when enabled).
101
- - POST `/api/auth/reset-password` accepts `{ token, password, confirmPassword }` and updates the user password while invalidating the token (only when enabled).
102
-
103
- Security notes:
104
-
105
- - The scaffold is intentionally disabled by default; enable it only for local testing or after hardening.
106
- - The forgot-password scaffold uses an in-memory rate limiter for demo purposes — replace it with a centralized rate limiter (Redis, API gateway) in production.
107
- - Always use a real email provider (SMTP, SendGrid, etc.) in production and never log reset tokens in server logs.
108
-
109
- Migration & upgrade notes
110
-
111
- - A recent schema migration changed PasswordReset to store tokenHash (SHA-256) instead of the raw token. Steps we executed:
112
- 1. Added `tokenHash` (nullable) to the PasswordReset model and made `token` nullable in Prisma.
113
- 2. Generated a migration that adds the tokenHash column and a unique index on it.
114
- 3. Ran a one-off script `scripts/populate-tokenhash.mjs` to compute SHA-256 hashes for previous tokens and fill `tokenHash`.
115
- 4. Applied the migration and regenerated the Prisma client (`npx prisma generate`).
116
-
117
- - To enable password reset behavior in your environment, set:
118
-
119
- ```
120
- NEXTWORKS_ENABLE_PASSWORD_RESET=1
121
- ```
122
-
123
- - Dev email helper (optional): there is an optional dev email helper (nodemailer + Ethereal) that can be enabled with `NEXTWORKS_USE_DEV_EMAIL=1`. When enabled (and NEXTWORKS_ENABLE_PASSWORD_RESET=1), forgot-password will send an Ethereal email and the server will log only the Ethereal preview URL (no plaintext token in logs). This helper is strictly opt-in for development only. Use a real email provider in production.
124
-
125
- - Production hardening: Password reset should only be enabled in production when a mail provider is configured. To harden the scaffold in production, ensure the following environment variables are set for SMTP (or equivalent for other providers):
126
-
127
- ```
128
- SMTP_HOST=smtp.example.com
129
- SMTP_PORT=587
130
- SMTP_USER=user@example.com
131
- SMTP_PASS=supersecret
132
- NOREPLY_EMAIL=no-reply@example.com
133
- NEXTWORKS_ENABLE_PASSWORD_RESET=1
134
- ```
135
-
136
- When NEXTWORKS_ENABLE_PASSWORD_RESET=1 and NODE_ENV=production the server will refuse to enable password reset unless a mail provider is configured. The server will not log reset tokens or URLs in production logs.
137
-
138
- Security checklist before enabling reset in production:
139
-
140
- - Set a strong NEXTAUTH_SECRET
141
- - Use HTTPS/TLS
142
- - Replace in-memory rate limiting with a centralized store (Redis/API Gateway)
143
- - Configure and test a real mail provider (SMTP, SendGrid, etc.)
144
- - Ensure password reset tokens are expired/cleaned up periodically
145
- - Review logging to avoid token leakage
146
-
147
- - Promotion script: to promote an existing user to admin, run:
148
-
149
- ```
150
- node ./scripts/promote-admin.mjs user@example.com
151
- ```
152
-
153
- - Important: Before turning password reset on in production, ensure you:
154
- - Replace the in-memory rate limiter with a centralized solution (Redis or API Gateway).
155
- - Configure a real email provider and remove console logging of tokens.
156
- - Ensure tokens are cleaned up periodically (see scripts/prune-password-resets.mjs stub).
157
- - Set a secure NEXTAUTH_SECRET and use HTTPS in production.
158
-
159
- ## Files & kit manifest (what to look at)
160
-
161
- If you want to inspect or package the Auth kit, the primary files and routes are listed below. This is a minimal manifest intended for documentation and CLI packaging reference — adjust paths if you extract the kit into another project.
162
-
163
- Key files
164
-
165
- - NextAuth handler & callbacks: lib/auth.ts
166
- - Prisma client: lib/prisma.ts
167
- - Auth helper utilities: lib/auth-helpers.ts, lib/hash.ts
168
- - Email providers: lib/email/index.ts, lib/email/dev-transport.ts, lib/email/provider-smtp.ts
169
- - Validation helpers: lib/validation/forms.ts
170
- - Form error mapping: lib/forms/map-errors.ts
171
- - UI components used by Auth: components/auth/_ and components/ui/_ (login-form.tsx, signup-form.tsx, button/input/label etc.)
172
- - Pages & API routes:
173
- - app/auth/login/page.tsx
174
- - app/auth/signup/page.tsx
175
- - app/auth/forgot-password/page.tsx
176
- - app/auth/reset-password/page.tsx
177
- - app/auth/verify-email/page.tsx
178
- - app/api/auth/[...nextauth]/route.ts
179
- - app/api/auth/forgot-password/route.ts
180
- - app/api/auth/reset-password/route.ts
181
- - app/api/auth/send-verify-email/route.ts
182
- - app/api/auth/providers/route.ts
183
- - app/api/signup/route.ts
184
- - Prisma schema & auth models: prisma/schema.prisma and prisma/auth-models.prisma
185
- - Seed & maintenance scripts: scripts/seed-demo.mjs, scripts/promote-admin.mjs, scripts/populate-tokenhash.mjs
186
-
187
- Minimal manifest (JSON)
188
-
189
- {
190
- "name": "auth-core",
191
- "files": [
192
- "lib/auth.ts",
193
- "lib/prisma.ts",
194
- "lib/auth-helpers.ts",
195
- "lib/hash.ts",
196
- "lib/forms/map-errors.ts",
197
- "lib/validation/forms.ts",
198
- "lib/email/index.ts",
199
- "components/auth/login-form.tsx",
200
- "components/auth/signup-form.tsx",
201
- "components/auth/logout-button.tsx",
202
- "components/session-provider.tsx",
203
- "app/auth/login/page.tsx",
204
- "app/auth/signup/page.tsx",
205
- "app/api/auth/[...nextauth]/route.ts",
206
- "app/api/signup/route.ts",
207
- "prisma/schema.prisma",
208
- "scripts/seed-demo.mjs",
209
- "scripts/promote-admin.mjs"
210
- ]
211
- }
212
-
213
- ## Where to customize NextAuth behavior
214
-
215
- - The NextAuth configuration and handlers live in lib/auth.ts. Common customizations include:
216
- - Adjusting providers (add/remove OAuth providers).
217
- - Modifying session and jwt callbacks to include or remove fields on the token/session.
218
- - Customizing pages (e.g., pages.signIn) or redirect logic.
219
-
220
- - Notes: lib/auth.ts already persists user.id and role into the JWT and exposes them on session.user for server components. If you change what is exposed on the session, ensure server-side checks (RBAC) are updated accordingly.
221
-
222
- ## CLI kit source (if packaging later)
223
-
224
- - A kit skeleton for packaging already exists in the repo at: `cli/kits/auth-core/` — it contains a compact copy of the core auth files (components, lib, prisma snippets) used by the CLI during development. Use that folder as a starting point when you implement the CLI packaging step.
225
-
226
- ## Post-install checklist (Auth)
227
-
228
- 1. Copy `.env.example` → `.env` and set DATABASE_URL, NEXTAUTH_URL, NEXTAUTH_SECRET (and GITHUB_ID/GITHUB_SECRET if using GitHub OAuth).
229
- 2. Generate Prisma client and run migrations:
230
-
231
- npx prisma generate
232
- npx prisma migrate dev -n init
233
-
234
- 3. (Optional) Seed demo data for quick testing:
235
-
236
- SEED_ADMIN_EMAIL=admin@example.com SEED_ADMIN_PASSWORD=password123 node ./scripts/seed-demo.mjs
237
-
238
- 4. Start dev server:
239
-
240
- npm run dev
241
-
242
- 5. Visit: `/auth/signup`, `/auth/login` and `/dashboard` for protected pages.
243
-
244
- If you want, I can also create a short docs/AUTH_MANIFEST.json file containing the JSON manifest above in the repo (useful for CLI packaging). I will not create CLI code or copy files unless you ask.
1
+ # Auth Quickstart
2
+
3
+ Follow these steps to get email/password auth (and optional GitHub OAuth) running in under 5 minutes.
4
+
5
+ If you are using the `nextworks` CLI in your own app, the recommended alpha setup is to install Blocks with sections and templates first, then Auth Core:
6
+
7
+ ```bash
8
+ npx nextworks add blocks --sections --templates
9
+ npx nextworks add auth-core
10
+ ```
11
+
12
+ Then follow the steps below inside your app.
13
+
14
+ ## 1) Copy environment variables
15
+
16
+ Create a `.env` file based on `.env.example`:
17
+
18
+ ```
19
+ cp .env.example .env
20
+ ```
21
+
22
+ Fill in values for the following environment variables used by the Auth kit:
23
+
24
+ - DATABASE_URL — PostgreSQL connection string
25
+ - NEXTAUTH_URL — URL of your app (e.g. http://localhost:3000 in dev)
26
+ - NEXTAUTH_SECRET — a strong random string (used to sign NextAuth tokens)
27
+
28
+ Optional / password reset / email provider vars:
29
+
30
+ - GITHUB_ID — (optional) GitHub OAuth client id
31
+ - GITHUB_SECRET — (optional) GitHub OAuth client secret
32
+ - NEXTWORKS_ENABLE_PASSWORD_RESET — set to `1` to enable the dev password reset scaffold (default: disabled)
33
+ - NEXTWORKS_USE_DEV_EMAIL — set to `1` to enable Ethereal dev email transport (for local testing only). **Works in `next dev` only.**
34
+ - SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASS, NOREPLY_EMAIL — when using a real SMTP provider (**required in production** when password reset is enabled). If not configured, `/api/auth/forgot-password` returns **404 by design**.
35
+ - NODE_ENV — (production/dev) used to guard password-reset behavior
36
+
37
+ Notes:
38
+
39
+ - The Auth kit will only enable the GitHub provider when both GITHUB_ID and GITHUB_SECRET are present.
40
+ - Password reset remains disabled by default; enable cautiously and only after configuring a real mail provider in production.
41
+
42
+ ## 2) Install and generate Prisma (required)
43
+
44
+ Auth Core requires Prisma (this kit is Prisma-coupled by design).
45
+
46
+ If you are using the monorepo directly:
47
+
48
+ ```bash
49
+ npm install
50
+ npx prisma generate
51
+ npx prisma migrate dev -n init
52
+ ```
53
+
54
+ If you installed Auth via the CLI (`npx nextworks add auth-core`):
55
+
56
+ - Prisma dependencies are added to your app via the kit's `package-deps.json` (so a plain `npm install` should be enough).
57
+ - You still must ensure `DATABASE_URL` is set, and you must run Prisma generate + migrations.
58
+
59
+ ```bash
60
+ npm install
61
+ npx prisma generate
62
+ npx prisma migrate dev -n init
63
+ ```
64
+
65
+ This applies the Prisma schema and generates the Prisma client.
66
+
67
+
68
+ ## 3) Start the dev server
69
+
70
+ ```
71
+ npm run dev
72
+ ```
73
+
74
+ ### Windows + Prisma + Next 16 (Turbopack) note
75
+
76
+ If you are on **Windows** and have installed the **Auth Core** kit (which uses Prisma under the hood) on **Next 16+**, the Next dev server may need permission to create symlinks for the Prisma client. On Windows, this can cause errors like:
77
+
78
+ > `create symlink to ../../../../node_modules/@prisma/client`
79
+ > `Caused by: A required privilege is not held by the client. (os error 1314)`
80
+
81
+ If you see this (often surfaced as a 500 on `/api/auth/session` or a NextAuth `CLIENT_FETCH_ERROR`), do one of the following:
82
+
83
+ - **Recommended**: Enable **Developer Mode** in Windows (Settings → For developers → Developer mode). This allows symlinks in normal terminals.
84
+ - Or: Run your dev server from an **elevated PowerShell/terminal** ("Run as administrator").
85
+
86
+ After doing one of these, restart `npm run dev` and reload your app.
87
+
88
+ Visit:
89
+
90
+ - http://localhost:3000/auth/signup to create a user
91
+ - http://localhost:3000/auth/login to sign in
92
+ - http://localhost:3000/dashboard after login
93
+
94
+ ## 4) Optional: GitHub OAuth
95
+
96
+ If you provided GITHUB_ID and GITHUB_SECRET in `.env`, the GitHub provider will be enabled and the GitHub button will appear on the forms. If not provided, the button will be hidden and only email/password is available.
97
+
98
+ ## 5) Roles (optional)
99
+
100
+ The `User` model has a `role` field (default: `user`). Role is propagated to the JWT and session so you can gate pages/components. You can later add admin tooling or seed scripts as needed.
101
+
102
+ ## 6) Seed and promote admin scripts
103
+
104
+ - `scripts/seed-demo.mjs` (already in the repo) creates a demo admin user and sample posts for quick demos.
105
+ - `scripts/promote-admin.mjs` (new) is idempotent and promotes an existing user to `admin`:
106
+
107
+ ```
108
+ node ./scripts/promote-admin.mjs admin@example.com
109
+ ```
110
+
111
+ ## 7) Forgot password (development scaffold)
112
+
113
+ This is a development scaffold that is disabled by default. To enable the password reset feature, set:
114
+
115
+ ```
116
+ NEXTWORKS_ENABLE_PASSWORD_RESET=1
117
+ ```
118
+
119
+ - POST `/api/auth/forgot-password` accepts `{ email }`. When enabled the endpoint generates a one-time token stored in the database and (in development) prints a reset link to the server console. The endpoint always returns a generic success message to avoid user enumeration.
120
+ - GET `/api/auth/reset-password?token=...` validates a token (only when enabled).
121
+ - POST `/api/auth/reset-password` accepts `{ token, password, confirmPassword }` and updates the user password while invalidating the token (only when enabled).
122
+
123
+ Security notes:
124
+
125
+ - The scaffold is intentionally disabled by default; enable it only for local testing or after hardening.
126
+ - The forgot-password scaffold uses an in-memory rate limiter for demo purposes — replace it with a centralized rate limiter (Redis, API gateway) in production.
127
+ - Always use a real email provider (SMTP, SendGrid, etc.) in production and never log reset tokens in server logs.
128
+
129
+ Migration & upgrade notes
130
+
131
+ - A recent schema migration changed PasswordReset to store tokenHash (SHA-256) instead of the raw token. Steps we executed:
132
+ 1. Added `tokenHash` (nullable) to the PasswordReset model and made `token` nullable in Prisma.
133
+ 2. Generated a migration that adds the tokenHash column and a unique index on it.
134
+ 3. Ran a one-off script `scripts/populate-tokenhash.mjs` to compute SHA-256 hashes for previous tokens and fill `tokenHash`.
135
+ 4. Applied the migration and regenerated the Prisma client (`npx prisma generate`).
136
+
137
+ - To enable password reset behavior in your environment, set:
138
+
139
+ ```
140
+ NEXTWORKS_ENABLE_PASSWORD_RESET=1
141
+ ```
142
+
143
+ - Dev email helper (optional): there is an optional dev email helper (nodemailer + Ethereal) that can be enabled with `NEXTWORKS_USE_DEV_EMAIL=1`. When enabled (and NEXTWORKS_ENABLE_PASSWORD_RESET=1), forgot-password will send an Ethereal email and the server will log only the Ethereal preview URL (no plaintext token in logs). This helper is strictly opt-in for development only. Use a real email provider in production.
144
+
145
+ - Production hardening: Password reset should only be enabled in production when a mail provider is configured. To harden the scaffold in production, ensure the following environment variables are set for SMTP (or equivalent for other providers):
146
+
147
+ ```
148
+ SMTP_HOST=smtp.example.com
149
+ SMTP_PORT=587
150
+ SMTP_USER=user@example.com
151
+ SMTP_PASS=supersecret
152
+ NOREPLY_EMAIL=no-reply@example.com
153
+ NEXTWORKS_ENABLE_PASSWORD_RESET=1
154
+ ```
155
+
156
+ When NEXTWORKS_ENABLE_PASSWORD_RESET=1 and NODE_ENV=production the server will refuse to enable password reset unless a mail provider is configured. The server will not log reset tokens or URLs in production logs.
157
+
158
+ Security checklist before enabling reset in production:
159
+
160
+ - Set a strong NEXTAUTH_SECRET
161
+ - Use HTTPS/TLS
162
+ - Replace in-memory rate limiting with a centralized store (Redis/API Gateway)
163
+ - Configure and test a real mail provider (SMTP, SendGrid, etc.)
164
+ - Ensure password reset tokens are expired/cleaned up periodically
165
+ - Review logging to avoid token leakage
166
+
167
+ - Promotion script: to promote an existing user to admin, run:
168
+
169
+ ```
170
+ node ./scripts/promote-admin.mjs user@example.com
171
+ ```
172
+
173
+ - Important: Before turning password reset on in production, ensure you:
174
+ - Replace the in-memory rate limiter with a centralized solution (Redis or API Gateway).
175
+ - Configure a real email provider and remove console logging of tokens.
176
+ - Ensure tokens are cleaned up periodically (see scripts/prune-password-resets.mjs stub).
177
+ - Set a secure NEXTAUTH_SECRET and use HTTPS in production.
178
+
179
+ ## Files & kit manifest (what to look at)
180
+
181
+ If you want to inspect or package the Auth kit, the primary files and routes are listed below. This is a minimal manifest intended for documentation and CLI packaging reference — adjust paths if you extract the kit into another project.
182
+
183
+ Key files
184
+
185
+ - NextAuth handler & callbacks: lib/auth.ts
186
+ - Prisma client: lib/prisma.ts
187
+ - Auth helper utilities: lib/auth-helpers.ts, lib/hash.ts
188
+ - Email providers: lib/email/index.ts, lib/email/dev-transport.ts, lib/email/provider-smtp.ts
189
+ - Validation helpers: lib/validation/forms.ts
190
+ - Form error mapping: lib/forms/map-errors.ts
191
+ - UI components used by Auth: components/auth/_ and components/ui/_ (login-form.tsx, signup-form.tsx, button/input/label etc.)
192
+ - Pages & API routes:
193
+ - app/auth/login/page.tsx
194
+ - app/auth/signup/page.tsx
195
+ - app/auth/forgot-password/page.tsx
196
+ - app/auth/reset-password/page.tsx
197
+ - app/auth/verify-email/page.tsx
198
+ - app/api/auth/[...nextauth]/route.ts
199
+ - app/api/auth/forgot-password/route.ts
200
+ - app/api/auth/reset-password/route.ts
201
+ - app/api/auth/send-verify-email/route.ts
202
+ - app/api/auth/providers/route.ts
203
+ - app/api/signup/route.ts
204
+ - Prisma schema & auth models: prisma/schema.prisma and prisma/auth-models.prisma
205
+ - Seed & maintenance scripts: scripts/seed-demo.mjs, scripts/promote-admin.mjs, scripts/populate-tokenhash.mjs
206
+
207
+ Minimal manifest (JSON)
208
+
209
+ {
210
+ "name": "auth-core",
211
+ "files": [
212
+ "lib/auth.ts",
213
+ "lib/prisma.ts",
214
+ "lib/auth-helpers.ts",
215
+ "lib/hash.ts",
216
+ "lib/forms/map-errors.ts",
217
+ "lib/validation/forms.ts",
218
+ "lib/email/index.ts",
219
+ "components/auth/login-form.tsx",
220
+ "components/auth/signup-form.tsx",
221
+ "components/auth/logout-button.tsx",
222
+ "components/session-provider.tsx",
223
+ "app/auth/login/page.tsx",
224
+ "app/auth/signup/page.tsx",
225
+ "app/api/auth/[...nextauth]/route.ts",
226
+ "app/api/signup/route.ts",
227
+ "prisma/schema.prisma",
228
+ "scripts/seed-demo.mjs",
229
+ "scripts/promote-admin.mjs"
230
+ ]
231
+ }
232
+
233
+ ## Where to customize NextAuth behavior
234
+
235
+ - The NextAuth configuration and handlers live in lib/auth.ts. Common customizations include:
236
+ - Adjusting providers (add/remove OAuth providers).
237
+ - Modifying session and jwt callbacks to include or remove fields on the token/session.
238
+ - Customizing pages (e.g., pages.signIn) or redirect logic.
239
+
240
+ - Notes: lib/auth.ts already persists user.id and role into the JWT and exposes them on session.user for server components. If you change what is exposed on the session, ensure server-side checks (RBAC) are updated accordingly.
241
+
242
+ ## CLI kit source (if packaging later)
243
+
244
+ - A kit skeleton for packaging already exists in the repo at: `cli/kits/auth-core/` it contains a compact copy of the core auth files (components, lib, prisma snippets) used by the CLI during development. Use that folder as a starting point when you implement the CLI packaging step.
245
+
246
+ ## Post-install checklist (Auth)
247
+
248
+ 1. Copy `.env.example` → `.env` and set DATABASE_URL, NEXTAUTH_URL, NEXTAUTH_SECRET (and GITHUB_ID/GITHUB_SECRET if using GitHub OAuth).
249
+ 2. Generate Prisma client and run migrations:
250
+
251
+ npx prisma generate
252
+ npx prisma migrate dev -n init
253
+
254
+ 3. (Optional) Seed demo data for quick testing:
255
+
256
+ SEED_ADMIN_EMAIL=admin@example.com SEED_ADMIN_PASSWORD=password123 node ./scripts/seed-demo.mjs
257
+
258
+ 4. Start dev server:
259
+
260
+ npm run dev
261
+
262
+ 5. Visit: `/auth/signup`, `/auth/login` and `/dashboard` for protected pages.
263
+
264
+ If you want, I can also create a short docs/AUTH_MANIFEST.json file containing the JSON manifest above in the repo (useful for CLI packaging). I will not create CLI code or copy files unless you ask.