create-tigra 3.3.0 → 4.0.1

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 (207) hide show
  1. package/CHANGELOG.md +151 -0
  2. package/README.md +264 -56
  3. package/bin/create-tigra.js +497 -472
  4. package/lib/patchers/email-verification.patcher.js +72 -10
  5. package/modules/email-verification/client/hooks/useVerification.ts +4 -1
  6. package/modules/email-verification/server/__tests__/verification.integration.test.ts +115 -0
  7. package/modules/email-verification/server/verification.controller.ts +3 -1
  8. package/modules/email-verification/server/verification.service.ts +44 -13
  9. package/package.json +5 -3
  10. package/template/.github/dependabot.yml +47 -0
  11. package/template/.github/workflows/audit.yml +36 -0
  12. package/template/.github/workflows/ci.yml +79 -0
  13. package/template/AGENTS.md +18 -9
  14. package/template/README.md +79 -0
  15. package/template/client/.dockerignore +1 -0
  16. package/template/client/.env.example.production +13 -2
  17. package/template/client/AGENTS.md +13 -5
  18. package/template/client/Dockerfile +186 -14
  19. package/template/client/README.md +92 -21
  20. package/template/client/next.config.ts +1 -1
  21. package/template/client/package-lock.json +15983 -14181
  22. package/template/client/package.json +56 -48
  23. package/template/client/scripts/check-sharp.mjs +53 -0
  24. package/template/client/scripts/next-dev.cjs +5 -1
  25. package/template/client/src/__tests__/proxy.test.ts +92 -0
  26. package/template/client/src/app/healthz/route.ts +16 -0
  27. package/template/client/src/app/layout.tsx +9 -3
  28. package/template/client/src/app/page.tsx +2 -2
  29. package/template/client/src/app/providers.tsx +23 -4
  30. package/template/client/src/components/layout/Header.tsx +7 -3
  31. package/template/client/src/features/admin/hooks/useAdminSessions.ts +2 -1
  32. package/template/client/src/features/admin/hooks/useAdminUsers.ts +3 -1
  33. package/template/client/src/features/admin/types/admin.types.ts +2 -3
  34. package/template/client/src/features/auth/components/AuthInitializer.tsx +35 -18
  35. package/template/client/src/features/auth/components/ForgotPasswordForm.tsx +107 -0
  36. package/template/client/src/features/auth/components/LimitNotice.tsx +28 -0
  37. package/template/client/src/features/auth/components/LoginForm.tsx +13 -2
  38. package/template/client/src/features/auth/components/RegisterForm.tsx +23 -7
  39. package/template/client/src/features/auth/hooks/useAuth.ts +11 -1
  40. package/template/client/src/features/auth/hooks/usePasswordReset.ts +7 -1
  41. package/template/client/src/features/auth/services/auth.service.ts +8 -3
  42. package/template/client/src/features/auth/store/authSlice.ts +7 -2
  43. package/template/client/src/lib/api/__tests__/axios.config.test.ts +183 -0
  44. package/template/client/src/lib/api/api.types.ts +14 -0
  45. package/template/client/src/lib/api/axios.config.ts +101 -15
  46. package/template/client/src/lib/api/hard-redirect.ts +8 -0
  47. package/template/client/src/lib/api/session-events.ts +29 -0
  48. package/template/client/src/lib/constants/routes.ts +12 -0
  49. package/template/client/src/lib/observability/sentry-scrub.ts +1 -1
  50. package/template/client/src/lib/utils/__tests__/error.test.ts +119 -0
  51. package/template/client/src/lib/utils/__tests__/form.test.ts +42 -0
  52. package/template/client/src/lib/utils/__tests__/paths.test.ts +43 -0
  53. package/template/client/src/lib/utils/__tests__/security.test.ts +40 -0
  54. package/template/client/src/lib/utils/__tests__/session-marker.test.ts +27 -0
  55. package/template/client/src/lib/utils/error.ts +81 -0
  56. package/template/client/src/lib/utils/form.ts +31 -0
  57. package/template/client/src/lib/utils/paths.ts +18 -0
  58. package/template/client/src/lib/utils/session-marker.ts +24 -0
  59. package/template/client/src/{middleware.ts → proxy.ts} +25 -11
  60. package/template/client/src/store/__tests__/store.test.ts +44 -0
  61. package/template/client/src/store/index.ts +26 -8
  62. package/template/client/vitest.config.mts +48 -0
  63. package/template/server/.env.example +280 -270
  64. package/template/server/.env.example.production +283 -242
  65. package/template/server/AGENTS.md +13 -4
  66. package/template/server/Dockerfile +176 -14
  67. package/template/server/README.md +108 -0
  68. package/template/server/docker-compose.yml +4 -2
  69. package/template/server/docs/deployment.md +63 -0
  70. package/template/server/docs/logging.md +66 -4
  71. package/template/server/docs/rate-limits.md +153 -0
  72. package/template/server/docs/testing.md +81 -0
  73. package/template/server/eslint.config.mjs +19 -0
  74. package/template/server/package-lock.json +8784 -8743
  75. package/template/server/package.json +79 -75
  76. package/template/server/prisma/migrations/0_init/migration.sql +82 -0
  77. package/template/server/prisma/migrations/migration_lock.toml +3 -0
  78. package/template/server/prisma/schema.prisma +0 -3
  79. package/template/server/scripts/check-heic.mjs +40 -0
  80. package/template/server/scripts/check-sharp.mjs +53 -0
  81. package/template/server/src/__tests__/routes.integration.test.ts +280 -0
  82. package/template/server/src/app.ts +248 -346
  83. package/template/server/src/config/__tests__/env.test.ts +219 -0
  84. package/template/server/src/config/__tests__/rate-limit.config.test.ts +158 -0
  85. package/template/server/src/config/env.ts +326 -182
  86. package/template/server/src/config/rate-limit.config.ts +157 -106
  87. package/template/server/src/jobs/__tests__/cleanup-deleted-accounts.job.test.ts +60 -8
  88. package/template/server/src/jobs/__tests__/cleanup-expired-auth.job.test.ts +44 -0
  89. package/template/server/src/jobs/__tests__/jobs.integration.test.ts +88 -0
  90. package/template/server/src/jobs/__tests__/schedule.test.ts +92 -0
  91. package/template/server/src/jobs/cleanup-deleted-accounts.job.ts +38 -36
  92. package/template/server/src/jobs/cleanup-expired-auth.job.ts +32 -24
  93. package/template/server/src/jobs/index.ts +12 -2
  94. package/template/server/src/jobs/refresh-cloudflare-ranges.job.ts +28 -0
  95. package/template/server/src/jobs/schedule.ts +50 -0
  96. package/template/server/src/libs/__tests__/auth.test.ts +98 -7
  97. package/template/server/src/libs/__tests__/client-ip.test.ts +257 -121
  98. package/template/server/src/libs/__tests__/cloudflare-ranges.test.ts +79 -0
  99. package/template/server/src/libs/__tests__/cookies.test.ts +53 -2
  100. package/template/server/src/libs/__tests__/email-quota.test.ts +81 -0
  101. package/template/server/src/libs/__tests__/email.test.ts +68 -0
  102. package/template/server/src/libs/__tests__/error-handler.test.ts +162 -0
  103. package/template/server/src/libs/__tests__/health-db-down.integration.test.ts +37 -0
  104. package/template/server/src/libs/__tests__/health-redis-down.integration.test.ts +45 -0
  105. package/template/server/src/libs/__tests__/health-routes.test.ts +83 -0
  106. package/template/server/src/libs/__tests__/health.integration.test.ts +118 -0
  107. package/template/server/src/libs/__tests__/http.test.ts +74 -2
  108. package/template/server/src/libs/__tests__/ip-block.test.ts +179 -62
  109. package/template/server/src/libs/__tests__/logger.test.ts +50 -0
  110. package/template/server/src/libs/__tests__/monitoring.test.ts +185 -0
  111. package/template/server/src/libs/__tests__/outbound.integration.test.ts +48 -0
  112. package/template/server/src/libs/__tests__/prisma-client.test.ts +28 -0
  113. package/template/server/src/libs/__tests__/query-counter.integration.test.ts +53 -0
  114. package/template/server/src/libs/__tests__/rate-limiting.test.ts +146 -0
  115. package/template/server/src/libs/__tests__/rate-limits.integration.test.ts +53 -0
  116. package/template/server/src/libs/__tests__/redis-required.test.ts +30 -0
  117. package/template/server/src/libs/__tests__/redis.test.ts +38 -2
  118. package/template/server/src/libs/__tests__/request-id.test.ts +25 -0
  119. package/template/server/src/libs/__tests__/requestLogger.test.ts +126 -0
  120. package/template/server/src/libs/__tests__/safe-http.test.ts +138 -0
  121. package/template/server/src/libs/__tests__/sentry-scrub.test.ts +82 -80
  122. package/template/server/src/libs/__tests__/shutdown.test.ts +89 -0
  123. package/template/server/src/libs/__tests__/url-safety.test.ts +130 -80
  124. package/template/server/src/libs/auth.ts +39 -10
  125. package/template/server/src/libs/client-ip.ts +158 -77
  126. package/template/server/src/libs/cloudflare-ranges.ts +132 -0
  127. package/template/server/src/libs/cookies.ts +53 -14
  128. package/template/server/src/libs/email-quota.ts +74 -0
  129. package/template/server/src/libs/email.ts +46 -9
  130. package/template/server/src/libs/error-handler.ts +111 -0
  131. package/template/server/src/libs/health-routes.ts +53 -0
  132. package/template/server/src/libs/http.ts +139 -47
  133. package/template/server/src/libs/ip-address.ts +37 -0
  134. package/template/server/src/libs/ip-block.ts +284 -221
  135. package/template/server/src/libs/logger.ts +41 -12
  136. package/template/server/src/libs/monitoring.ts +127 -41
  137. package/template/server/src/libs/observability/sentry.ts +1 -1
  138. package/template/server/src/libs/origin-check.ts +9 -7
  139. package/template/server/src/libs/prisma.ts +47 -4
  140. package/template/server/src/libs/query-counter.ts +71 -59
  141. package/template/server/src/libs/rate-limiting.ts +110 -0
  142. package/template/server/src/libs/redis-required.ts +28 -0
  143. package/template/server/src/libs/redis.ts +27 -1
  144. package/template/server/src/libs/request-id.ts +18 -0
  145. package/template/server/src/libs/requestLogger.ts +112 -17
  146. package/template/server/src/libs/safe-http.ts +101 -0
  147. package/template/server/src/libs/shutdown.ts +87 -0
  148. package/template/server/src/libs/storage/__tests__/fixtures/tiny-heic.ts +13 -0
  149. package/template/server/src/libs/storage/__tests__/image-optimizer.test.ts +59 -0
  150. package/template/server/src/libs/storage/__tests__/multipart-limits.test.ts +91 -0
  151. package/template/server/src/libs/storage/__tests__/upload-persistence.test.ts +52 -0
  152. package/template/server/src/libs/storage/file-storage.service.ts +94 -84
  153. package/template/server/src/libs/storage/file-validator.ts +48 -46
  154. package/template/server/src/libs/storage/image-optimizer.service.ts +16 -5
  155. package/template/server/src/libs/token-hash.ts +18 -0
  156. package/template/server/src/libs/url-safety.ts +127 -121
  157. package/template/server/src/libs/validation-errors.ts +86 -0
  158. package/template/server/src/modules/admin/__tests__/admin-actions.integration.test.ts +255 -0
  159. package/template/server/src/modules/admin/__tests__/admin.health.test.ts +82 -0
  160. package/template/server/src/modules/admin/__tests__/admin.repo.test.ts +55 -0
  161. package/template/server/src/modules/admin/__tests__/admin.schemas.test.ts +38 -0
  162. package/template/server/src/modules/admin/__tests__/admin.service.test.ts +162 -0
  163. package/template/server/src/modules/admin/admin.controller.ts +66 -49
  164. package/template/server/src/modules/admin/admin.repo.ts +49 -10
  165. package/template/server/src/modules/admin/admin.routes.ts +48 -2
  166. package/template/server/src/modules/admin/admin.schemas.ts +18 -6
  167. package/template/server/src/modules/admin/admin.service.ts +66 -4
  168. package/template/server/src/modules/auth/__tests__/auth.controller.test.ts +39 -0
  169. package/template/server/src/modules/auth/__tests__/auth.integration.test.ts +9 -3
  170. package/template/server/src/modules/auth/__tests__/auth.service.dummy-hash.test.ts +34 -0
  171. package/template/server/src/modules/auth/__tests__/auth.service.test.ts +197 -37
  172. package/template/server/src/modules/auth/__tests__/session-cleanup.test.ts +39 -0
  173. package/template/server/src/modules/auth/__tests__/sessions.integration.test.ts +246 -0
  174. package/template/server/src/modules/auth/__tests__/token-fingerprints.test.ts +119 -0
  175. package/template/server/src/modules/auth/auth.controller.ts +133 -128
  176. package/template/server/src/modules/auth/auth.repo.ts +44 -44
  177. package/template/server/src/modules/auth/auth.schemas.ts +4 -2
  178. package/template/server/src/modules/auth/auth.service.ts +167 -96
  179. package/template/server/src/modules/auth/session.repo.ts +12 -0
  180. package/template/server/src/modules/files/__tests__/files.integration.test.ts +5 -5
  181. package/template/server/src/modules/users/__tests__/avatar-upload.test.ts +120 -0
  182. package/template/server/src/modules/users/__tests__/name-rules.test.ts +46 -0
  183. package/template/server/src/modules/users/__tests__/users.integration.test.ts +295 -0
  184. package/template/server/src/modules/users/__tests__/users.repo.test.ts +37 -0
  185. package/template/server/src/modules/users/__tests__/users.service.test.ts +129 -0
  186. package/template/server/src/modules/users/users.controller.ts +9 -38
  187. package/template/server/src/modules/users/users.repo.ts +23 -7
  188. package/template/server/src/modules/users/users.routes.ts +3 -22
  189. package/template/server/src/modules/users/users.schemas.ts +15 -9
  190. package/template/server/src/modules/users/users.service.ts +105 -93
  191. package/template/server/src/scripts/make-admin.ts +48 -0
  192. package/template/server/src/server.ts +48 -26
  193. package/template/server/src/shared/errors/AppError.ts +27 -1
  194. package/template/server/src/shared/errors/errors.ts +42 -5
  195. package/template/server/src/shared/responses/successResponse.ts +22 -3
  196. package/template/server/src/shared/types/index.ts +7 -0
  197. package/template/server/src/test/__tests__/integration-services.test.ts +66 -0
  198. package/template/server/src/test/integration-helpers.ts +343 -0
  199. package/template/server/src/test/integration-services.ts +74 -0
  200. package/template/server/src/test/integration.global-setup.ts +144 -0
  201. package/template/server/src/test/integration.setup.ts +35 -158
  202. package/template/server/src/test/setup.ts +3 -10
  203. package/template/server/tsconfig.build.json +1 -1
  204. package/template/server/tsconfig.json +1 -0
  205. package/template/server/vitest.config.ts +17 -4
  206. package/template/server/vitest.integration.config.ts +18 -13
  207. package/template/server/src/libs/storage/filename-sanitizer.ts +0 -71
@@ -1,4 +1,6 @@
1
- # Tigra Codex instructions
1
+ # Agent instructions
2
+
3
+ **Read the scoped file first.** Before changing anything in `server/`, read `server/AGENTS.md`; before changing anything in `client/`, read `client/AGENTS.md`. Claude Code loads them by itself when it opens files there. Codex does not: it loads only the AGENTS.md files from the project root down to the folder it was started in, so started here it never sees them.
2
4
 
3
5
  ## MUST FOLLOW — comment important code
4
6
 
@@ -33,30 +35,37 @@ Every implementation change MUST include a human-readability pass. Add or update
33
35
 
34
36
  - Use functional kebab-case for project names and anything surfaced in CLIs, logs, queues, jobs, or worker/process names.
35
37
  - Use camelCase for TypeScript values/functions, PascalCase for components/types/classes, and UPPER_SNAKE_CASE for true constants.
36
- - Keep domain folders and route paths lowercase and functional; follow the scoped client/server naming rules below.
38
+ - Keep domain folders and route paths lowercase and functional; the file naming rules of each app are in `server/AGENTS.md` and `client/AGENTS.md`.
37
39
 
38
40
  ## Deployment boundaries
39
41
 
40
42
  - The Dockerfile in each app is its production contract. Re-check it when changing ports, entry points, build output, native dependencies, runtime files, health routes, or environment variables.
41
- - The client must retain Next.js `output: "standalone"`; every `NEXT_PUBLIC_*` value is public and must be available at Docker build time.
42
- - The server container applies `prisma migrate deploy` before startup and exposes unauthenticated `GET /api/v1/live` for orchestration.
43
+ - The client must retain Next.js `output: "standalone"`; every `NEXT_PUBLIC_*` value is public and must be available at Docker build time. The client build stops when `NEXT_PUBLIC_API_BASE_URL` is empty. Its health check is `GET /healthz` (`client/src/app/healthz/route.ts`), which must keep answering 200 without rendering a page.
44
+ - The server container applies `prisma migrate deploy` before startup and exposes unauthenticated `GET /api/v1/live` for orchestration. It refuses to start when the image has no migrations (the database would have no tables), so keep `server/prisma/migrations/` committed, starting with the shipped `0_init`, and never switch to `prisma db push` for production. In production it also refuses to start when Redis is not reachable within 10 s: rate limits, login lockout and IP blocks run on Redis.
45
+ - Every deploy applies the new migrations while the old version is still serving, so a migration must work with the code that is still running. Follow expand / deploy / contract (`server/docs/deployment.md`): never drop, rename, retype or make a column required in the same deploy as the code that stops using it.
46
+ - Both Dockerfiles build sharp against a pinned libvips so images run on CPUs without x86-64-v2. When upgrading sharp (or Next, which brings its own sharp), check the new sharp's `config.libvips` range; if `LIBVIPS_VERSION` no longer satisfies it, bump `LIBVIPS_VERSION` and `LIBVIPS_SHA256` in both Dockerfiles. The build fails with a clear message if they do not match.
47
+ - Write `package-lock.json` with npm 11.19 or newer (`npm -v`). The images run `npm ci` with npm 11.19.1, which rejects lockfiles from npm 10 and from early npm 11 (11.6.1 was seen failing).
43
48
  - `server/docker-compose.yml` is local-development infrastructure, not a production topology. Keep host ports loopback-bound and admin UIs opt-in through the `tools` profile.
44
49
 
45
50
  ## Non-obvious security rules
46
51
 
47
52
  - Never commit `.env` files, credentials, tokens, or production data. Never place a secret in `NEXT_PUBLIC_*` or client-side code.
48
53
  - Authentication uses server-issued httpOnly cookies. Do not copy tokens into localStorage, sessionStorage, Redux, logs, URLs, or response bodies.
49
- - Validate every external input at the server boundary with Zod, use Prisma parameterization, enforce authorization in server services/routes, and return user-safe errors.
54
+ - Validate every external input at the server boundary with Zod, use Prisma parameterization, enforce authorization in server services/routes, and return user-safe errors. Wrong input always answers 422 `VALIDATION_FAILED` with `error.fields` (field + message, never the submitted values); the client reads them with `getFieldErrors`.
50
55
  - Preserve origin/CORS checks for state-changing cookie-authenticated requests, rate limits on public/auth-sensitive endpoints, and the split between statically served public uploads and authenticated owner-scoped private files.
51
56
 
52
- ## Required server security workflow
57
+ ## Required security workflow
53
58
 
54
59
  - For every meaningful server implementation, bug fix, refactor, dependency update, or configuration change, use the repository-local `security-audit` skill in guidance mode while designing and reviewing the change. Treat authentication, authorization, validation, database access, outbound requests, uploads, cookies, CORS/origin handling, rate limits, environment configuration, and deployment behavior as security-sensitive surfaces.
55
- - Before completion, review the current working-tree patch with the agent's own security scan when available: in Codex use the Codex Security plugin's `codex-security:security-diff-scan` skill; in Claude Code run `/security-review`. Scope the review to the changed server behavior and the supporting code needed to establish impact; do not expand it into an unrelated repository audit.
56
- - If that scan is unavailable, perform an equivalent focused patch review with the bundled `security-audit` skill and explicitly report that the scan was unavailable. Do not skip the security gate silently.
60
+ - The same workflow applies to client changes in its security code: `client/src/proxy.ts` (security headers, CSP, route protection), the sign-in flow and its redirects (`client/src/features/auth/`), the shared API client (`client/src/lib/api/axios.config.ts`), and anything that renders HTML or untrusted URLs.
61
+ - Before completion, review the patch you are handing over: `git diff HEAD` plus every untracked file (`git status --porcelain`). In Codex, use the Codex Security plugin's `codex-security:security-diff-scan` skill when it is installed. In Claude Code, run the bundled `security-audit` skill in guidance mode on that diff. Claude Code's `/security-review` is not this check: it reviews only `git diff origin/HEAD...` (commits since the remote branch), so it never sees uncommitted work, and a project that was never pushed has no `origin/HEAD` at all. Scope the review to the changed behavior and the supporting code needed to establish impact; do not expand it into an unrelated repository audit.
62
+ - If Codex's scan is unavailable, perform the same focused patch review with the bundled `security-audit` skill and explicitly report that the scan was unavailable. Do not skip the security gate silently.
57
63
  - A full or deep repository audit remains explicit work: do not start one merely because ordinary server code changed. Fix confirmed in-scope findings, rerun affected checks, and disclose any unresolved or unverified security concern before claiming completion.
58
64
  - Documentation-only, comment-only, and formatting-only changes may skip the patch scan when they cannot affect runtime behavior, permissions, deployment, or security policy.
59
65
 
60
66
  ## Verification
61
67
 
62
- Run the smallest relevant lint, typecheck, test, and build commands in each affected app. Database and deployment changes also require migration/Docker compatibility checks.
68
+ - Server (`server/`): `npm run lint`, `npm run typecheck`, `npm test` (unit), `npm run test:coverage` (fails below the floor in `vitest.config.ts`; do not lower it to make a change pass), `npm run test:integration`, `npm run build`. Client (`client/`): `npm run lint`, `npm run typecheck`, `npm test` (unit), `npm run test:coverage` (same floor rule, `vitest.config.mts`), `npm run build`. Run the ones that cover what you changed.
69
+ - `npm run test:integration` needs a real MySQL and Redis: either `INTEGRATION_DATABASE_URL` (a database whose name ends in `_test`) and `INTEGRATION_REDIS_URL` (Redis database 1-15), or Docker for Testcontainers. If neither is available, say that the integration tests did not run instead of reporting the change as tested.
70
+ - Never run `npm run prisma:reset` (`prisma migrate reset`) or `prisma db push` against a database that was not created for the test: they delete or rewrite its data. The integration suite empties its own test database.
71
+ - Database and deployment changes also require migration/Docker compatibility checks.
@@ -0,0 +1,79 @@
1
+ # {{PROJECT_DISPLAY_NAME}}
2
+
3
+ A full-stack TypeScript app generated with [create-tigra](https://github.com/blessandsoul/create-tigra): a Fastify
4
+ API and a Next.js client that run and deploy separately.
5
+
6
+ ```text
7
+ {{PROJECT_NAME}}/
8
+ ├── server/ the API: Fastify 5, Prisma (MySQL 8.4), Redis 7 → server/README.md
9
+ ├── client/ the web app: Next.js 16, React 19, Tailwind v4 → client/README.md
10
+ ├── .github/ CI on every push, a weekly dependency audit, Dependabot
11
+ └── AGENTS.md the rules for changing this code (AI agents read it; so can you)
12
+ ```
13
+
14
+ ## Run it locally
15
+
16
+ You need Node.js 24+ with npm 11.19+ (`npm -v`), and Docker for MySQL and Redis.
17
+
18
+ ```bash
19
+ cd server
20
+ npm ci # the shipped lockfile, exactly
21
+ npm run docker:up # this project's MySQL and Redis
22
+ npm run prisma:generate
23
+ npm run prisma:migrate:dev # creates the tables from the first migration
24
+ npm run dev
25
+
26
+ # second terminal
27
+ cd client
28
+ npm ci
29
+ npm run dev
30
+ ```
31
+
32
+ | What | Where |
33
+ |---|---|
34
+ | Web app | http://localhost:3000 |
35
+ | API | http://localhost:8000/api/v1 (health: `/api/v1/health`) |
36
+ | MySQL | `127.0.0.1:{{MYSQL_PORT}}`, database `{{DATABASE_NAME}}` |
37
+ | Redis | `127.0.0.1:{{REDIS_PORT}}` |
38
+ | phpMyAdmin / Redis Commander | http://localhost:{{PHPMYADMIN_PORT}} / http://localhost:{{REDIS_COMMANDER_PORT}}, after `npm run docker:tools` |
39
+
40
+ `server/.env` and `client/.env` were written with fresh secrets when the project was created; they are git-ignored.
41
+ Every setting is explained in `server/.env.example` and `client/.env.example`.
42
+
43
+ ## Before you build features
44
+
45
+ - **The client has no pages yet, on purpose.** It ships the wiring (API client, sign-in flow, forms, security
46
+ headers) and you design the screens. Add pages at the paths in `client/src/lib/constants/routes.ts`: `/login`,
47
+ `/register`, `/reset-password`, `/dashboard`, `/profile`, `/admin`. The proxy and the emails already send people
48
+ there. See `client/README.md`.
49
+ - **The first admin:** sign up, then `npm run make-admin -- you@example.com` in `server/` (in production:
50
+ `node dist/scripts/make-admin.js you@example.com` in the server's terminal).
51
+ - **Emails** (password reset, and verification if it is on) need `RESEND_API_KEY` in `server/.env`.
52
+
53
+ ## Before you push
54
+
55
+ ```bash
56
+ cd server && npm run lint && npm run typecheck && npm run test:coverage && npm run test:integration && npm run build
57
+ cd client && npm run lint && npm run typecheck && npm run test:coverage && npm run build
58
+ ```
59
+
60
+ CI (`.github/workflows/ci.yml`) runs the same on every push and pull request. The integration tests need Docker, or a
61
+ MySQL and Redis you already run (`server/docs/testing.md`).
62
+
63
+ ## Deploy
64
+
65
+ Two Coolify applications from this repository, one per Dockerfile (`server/`, `client/`), plus a MySQL and a Redis.
66
+ Deploy the server first: it applies the migrations at start. The settings, the variables and what a deploy does:
67
+ `server/docs/deployment.md`, and the checklist in the
68
+ [create-tigra README](https://github.com/blessandsoul/create-tigra#deploying-to-coolify).
69
+
70
+ ## Guides
71
+
72
+ | File | What it explains |
73
+ |---|---|
74
+ | `server/README.md`, `client/README.md` | each app's layout, commands and conventions |
75
+ | `server/docs/deployment.md` | what a deploy does, safe migrations, rollback |
76
+ | `server/docs/rate-limits.md` | rate limits, login lockout, IP blocks, email caps |
77
+ | `server/docs/testing.md` | unit and integration tests, CI, coverage |
78
+ | `server/docs/logging.md` | request logs, redaction, health checks |
79
+ | `AGENTS.md`, `server/AGENTS.md`, `client/AGENTS.md` | the rules for changing the code |
@@ -39,6 +39,7 @@ coverage
39
39
  **/*.spec.ts
40
40
  **/*.spec.tsx
41
41
  vitest.config.ts
42
+ vitest.config.mts
42
43
  jest.config.ts
43
44
 
44
45
  # Documentation
@@ -1,5 +1,16 @@
1
- # Public API URL - points to the production server's base URL
1
+ # ===================================================================
2
+ # CLIENT APP (in Coolify: the client application only, not the server)
3
+ # ===================================================================
4
+ # Every NEXT_PUBLIC_* value is PUBLIC: Next.js bakes it into the JavaScript
5
+ # that every visitor downloads, at BUILD time. So, unlike the server's
6
+ # variables (which must never be available at build time), each of these
7
+ # needs "Available at Buildtime" ticked in Coolify (keep it available at
8
+ # runtime too). Never put a secret here. Changing one needs a redeploy, which
9
+ # rebuilds the image; a restart is not enough.
10
+
11
+ # The server's public API address, ending in /api/v1. Required: the image
12
+ # build stops when it is empty (client/Dockerfile).
2
13
  NEXT_PUBLIC_API_BASE_URL=https://api.yourdomain.com/api/v1
3
14
 
4
- # App name displayed in the UI
15
+ # App name shown in the UI. Optional (defaults to "My App").
5
16
  NEXT_PUBLIC_APP_NAME={{PROJECT_DISPLAY_NAME}}
@@ -1,4 +1,4 @@
1
- # Client Codex instructions
1
+ # Client agent instructions
2
2
 
3
3
  ## Architecture and naming
4
4
 
@@ -10,14 +10,22 @@
10
10
  ## Sources of truth
11
11
 
12
12
  - Server-rendered page data belongs in Server Components; client-fetched server data belongs in React Query; Redux is limited to the authenticated-user lifecycle; local and URL state stay local or in search params.
13
- - `src/lib/constants/routes.ts` and `api-endpoints.ts` own application and API paths.
13
+ - Redux has one store per visitor (`makeStore` in `src/store/index.ts`, created in `Providers`). Never import a store instance into a module: the API client reports a session that ended through `src/lib/api/session-events.ts`, and `AuthInitializer` clears the user.
14
+ - `src/lib/constants/routes.ts` and `api-endpoints.ts` own application and API paths. `PROTECTED_PATHS` and `AUTH_PATHS` there are the one list each that the proxy and `AuthInitializer` use, matched by whole path segments (`src/lib/utils/paths.ts`): add your protected areas there.
15
+ - The start-up "who am I" check runs only when the browser holds the server's `auth_session` marker (`src/lib/utils/session-marker.ts`), so anonymous visitors make no API calls.
14
16
  - `src/styles/themes/default.css` owns color values. Components use semantic Tailwind tokens; do not hardcode palette values or create competing theme variables.
15
17
  - If a `next/font` CSS variable is referenced by a token declared on `:root`, apply that font's generated variable class to `<html>`. Never attach it only to `<body>`, where it cannot resolve the root-scoped token.
16
- - `src/middleware.ts` is the single source of truth for security headers and nonce-based CSP. Do not add a second CSP in `next.config.ts`.
18
+ - `src/proxy.ts` (Next 16's name for middleware) is the single source of truth for security headers and nonce-based CSP. Do not add a second CSP in `next.config.ts`.
17
19
 
18
20
  ## Security and deployment
19
21
 
20
- - Treat client validation as UX only; the server must validate and authorize every request.
22
+ - Treat client validation as UX only; the server must validate and authorize every request. Forms put the server's 422 messages on their own fields with `applyFieldErrors` (`src/lib/utils/form.ts`); keep `RegisterForm`'s rules equal to the server's `registerSchema`.
21
23
  - Use the shared credentialed API client. Auth tokens remain in httpOnly cookies and must never enter browser storage or Redux.
22
24
  - Sanitize any intentionally rendered HTML and untrusted URLs. Never log passwords, tokens, credentials, or complete sensitive user objects.
23
- - Preserve `output: "standalone"`, the non-root container, and the root health check. Add every new public environment variable to the Docker builder as `ARG` + `ENV`; public variables are compiled into the bundle.
25
+ - Preserve `output: "standalone"`, the non-root container, and the `/healthz` health check (`src/app/healthz/route.ts`, used by the Dockerfile HEALTHCHECK; keep it answering 200 without rendering a page). Add every new public environment variable to the Docker builder as `ARG` + `ENV`; public variables are compiled into the bundle. The build stops when `NEXT_PUBLIC_API_BASE_URL` is empty (in Coolify it needs "Available at Buildtime").
26
+ - Changes to `src/proxy.ts`, the sign-in flow and its redirects (`src/features/auth/`), the shared API client (`src/lib/api/axios.config.ts`), or anything that renders HTML or untrusted URLs go through the root `AGENTS.md` security workflow.
27
+
28
+ ## Tests
29
+
30
+ - `npm test` (and `npm run test:coverage`, which CI runs and which fails below the floor in `vitest.config.mts`) runs the unit tests in `src/**/__tests__/`. Keep logic in functions under `src/lib/` and test it there; a test file that needs `document` or `window` starts with `// @vitest-environment jsdom`.
31
+ - The template ships no pages on purpose. Screens are proven by hand in a browser against a running server; the logic under them is what the unit tests cover.
@@ -8,11 +8,86 @@
8
8
  # across builds, so a lockfile/dep-change rebuild re-installs from cache
9
9
  # (measured ~2.8x: 49s->18s on a real create-tigra client) instead of
10
10
  # re-downloading every tarball. Requires the `# syntax` line above (BuildKit).
11
+ #
12
+ # Every stage uses the same pinned node:24-alpine image (Node 24.22.0,
13
+ # npm 11.19.1, Alpine 3.24). Node 24 is the template's runtime (package.json
14
+ # engines >=24). `npm ci` below fails when package-lock.json was written by an
15
+ # older npm: npm 10 always, and early npm 11 too (11.6.1 leaves out the
16
+ # @emnapi/* entries this npm requires). Write lockfiles with npm 11.19 or newer.
17
+ #
18
+ # next/image optimizes images at runtime with sharp (GET /_next/image), so the
19
+ # client needs the same CPU-safe sharp as the server: stages 1-2 below.
11
20
 
12
21
  # ===================================================================
13
- # Stage 1: Dependencies (cached layer)
22
+ # Stage 1: libvips that runs on any x86-64 CPU
14
23
  # ===================================================================
15
- FROM node:20-alpine@sha256:fb4cd12c85ee03686f6af5362a0b0d56d50c58a04632e6c0fb8363f609372293 AS dependencies
24
+ # Why this stage exists: sharp's prebuilt addon and the libvips it bundles
25
+ # need an x86-64-v2 CPU (SSE4.2/POPCNT). Many VPS hosts present an older
26
+ # virtual CPU (e.g. "QEMU Virtual CPU"), where the prebuilt sharp refuses to
27
+ # load and the server crashes at start. So we compile a pinned libvips release
28
+ # for the generic x86-64 baseline and build sharp's addon against it (stage 2).
29
+ #
30
+ # KEEP THIS STAGE BYTE-IDENTICAL in server/Dockerfile and client/Dockerfile,
31
+ # so one rule set builds both images. Identical text also lets a plain
32
+ # `docker build` host reuse the compiled layer across the two apps. On
33
+ # Coolify (measured): redeploys of the same app on the same server reused it,
34
+ # but the two apps did not share it, and a change to a build-time variable
35
+ # compiled it again (Coolify passes those into the build). An uncached
36
+ # compile takes about 6-7 min on a small shared VPS.
37
+ #
38
+ # Version coupling: every sharp release needs a minimum libvips (the
39
+ # "config.libvips" range in sharp's package.json, e.g. ">=8.18.7" for sharp
40
+ # 0.35.5). When a sharp upgrade needs more than LIBVIPS_VERSION, bump
41
+ # LIBVIPS_VERSION and LIBVIPS_SHA256 (from the libvips release page) in BOTH
42
+ # Dockerfiles. Stage 2 stops the build with a clear message if you forget.
43
+ FROM node:24-alpine@sha256:44fce696f38ba8c987c4db89954051000bbd05ffd8d8659ba15ed4a8602e937f AS libvips
44
+
45
+ ARG LIBVIPS_VERSION=8.18.7
46
+ ARG LIBVIPS_SHA256=5baaead3b0bb20ffdb9e9ff09aa9fda08620923df77b63b436654cb5e0b3bf94
47
+
48
+ # Compilers and codec headers. python3 serves node-gyp in the stage that
49
+ # builds FROM this one. Every -dev package here needs its runtime library in
50
+ # the production stage's apk list (keep the two lists in sync).
51
+ RUN apk add --no-cache \
52
+ build-base python3 curl xz meson ninja pkgconf \
53
+ glib-dev expat-dev libjpeg-turbo-dev libpng-dev libwebp-dev \
54
+ tiff-dev lcms2-dev libheif-dev libexif-dev
55
+
56
+ ENV PKG_CONFIG_PATH=/opt/vips/lib/pkgconfig \
57
+ LD_LIBRARY_PATH=/opt/vips/lib
58
+
59
+ # --auto-features=disabled turns every optional dependency off; only the
60
+ # formats we use are enabled: JPEG/PNG/WebP/HEIF/TIFF (plus the built-in GIF
61
+ # loader, which Next's image optimizer accepts), colour profiles, and EXIF
62
+ # (needed to read orientation and strip metadata). The rarely used PPM,
63
+ # Analyze and Radiance loaders are off to shrink the parser attack surface.
64
+ # -march=x86-64 is the baseline every x86-64 CPU supports (other
65
+ # architectures keep the compiler default). -j 2 bounds memory on small
66
+ # build hosts.
67
+ RUN set -eu; \
68
+ if [ "$(uname -m)" = "x86_64" ]; then \
69
+ export CFLAGS="-march=x86-64 -mtune=generic" CXXFLAGS="-march=x86-64 -mtune=generic"; \
70
+ fi; \
71
+ curl -fsSL --retry 3 -o /tmp/vips.tar.xz \
72
+ "https://github.com/libvips/libvips/releases/download/v${LIBVIPS_VERSION}/vips-${LIBVIPS_VERSION}.tar.xz"; \
73
+ echo "${LIBVIPS_SHA256} /tmp/vips.tar.xz" | sha256sum -c -; \
74
+ tar -xJf /tmp/vips.tar.xz -C /tmp; \
75
+ meson setup /tmp/vips-build "/tmp/vips-${LIBVIPS_VERSION}" \
76
+ --prefix=/opt/vips --libdir=lib --buildtype=release --auto-features=disabled \
77
+ -Djpeg=enabled -Dpng=enabled -Dwebp=enabled -Dheif=enabled -Dtiff=enabled \
78
+ -Dlcms=enabled -Dzlib=enabled -Dexif=enabled \
79
+ -Dexamples=false -Dppm=false -Danalyze=false -Dradiance=false; \
80
+ meson compile -C /tmp/vips-build -j 2; \
81
+ meson install -C /tmp/vips-build; \
82
+ test "$(pkg-config --modversion vips-cpp)" = "${LIBVIPS_VERSION}"; \
83
+ rm -rf /tmp/vips-build "/tmp/vips-${LIBVIPS_VERSION}" /tmp/vips.tar.xz
84
+
85
+ # ===================================================================
86
+ # Stage 2: Dependencies + CPU-safe sharp addon
87
+ # ===================================================================
88
+ # Built FROM the libvips stage because compiling sharp's addon needs the
89
+ # compilers and every codec -dev package that vips-cpp.pc references.
90
+ FROM libvips AS dependencies
16
91
 
17
92
  WORKDIR /app
18
93
 
@@ -29,17 +104,66 @@ RUN --mount=type=cache,target=/root/.npm \
29
104
  npm ci; \
30
105
  fi
31
106
 
107
+ # Build sharp's native addon against /opt/vips. sharp loads
108
+ # node_modules/sharp/src/build/Release/sharp-<platform>-<version>.node BEFORE
109
+ # its prebuilt addon and without the prebuilt's x86-64-v2 check, so this file
110
+ # is what makes sharp work on older CPUs. Keep this block identical in
111
+ # server/Dockerfile and client/Dockerfile.
112
+ # - It runs after every npm install above, so it targets the sharp version
113
+ # actually installed (read from its package.json, never hard-coded).
114
+ # - SHARP_FORCE_GLOBAL_LIBVIPS makes sharp skip its own libvips version
115
+ # check, so the block checks sharp's "config.libvips" range itself.
116
+ # - node-addon-api and node-gyp are build tools sharp requires but does not
117
+ # install. They go to /tmp/sharp-build (exact pins inside sharp's declared
118
+ # ranges; --ignore-scripts, since neither needs an install script and their
119
+ # unlocked transitive packages must not run code here) and never reach the
120
+ # lockfile or the runtime image.
121
+ # - npm_config_nodedir points node-gyp at this image's own Node headers, so
122
+ # nothing is downloaded and the addon matches the runtime Node exactly.
123
+ # - Intermediate build files are dropped; only the .node file stays, also
124
+ # staged in /opt/sharp-addon for stages that copy it explicitly.
125
+ RUN --mount=type=cache,target=/root/.npm \
126
+ set -eu; \
127
+ npm install --prefix /tmp/sharp-build --no-save --no-audit --no-fund --ignore-scripts \
128
+ node-addon-api@8.9.2 node-gyp@12.4.0; \
129
+ if [ ! -d node_modules/sharp ]; then \
130
+ echo "ERROR: node_modules/sharp is missing. If this app no longer uses sharp, delete this build step and the libvips stage." >&2; \
131
+ exit 1; \
132
+ fi; \
133
+ cd node_modules/sharp; \
134
+ version="$(node -p "require('./package.json').version")"; \
135
+ need="$(node -p "require('./package.json').config.libvips")"; \
136
+ have="$(pkg-config --modversion vips-cpp)"; \
137
+ if ! node -e "process.exit(require('semver').satisfies(process.argv[1], process.argv[2]) ? 0 : 1)" "$have" "$need"; then \
138
+ echo "ERROR: sharp ${version} needs libvips ${need}, but this image builds libvips ${have}. Raise LIBVIPS_VERSION and LIBVIPS_SHA256 in BOTH server/Dockerfile and client/Dockerfile." >&2; \
139
+ exit 1; \
140
+ fi; \
141
+ if [ "$(uname -m)" = "x86_64" ]; then \
142
+ export CFLAGS="-march=x86-64 -mtune=generic" CXXFLAGS="-march=x86-64 -mtune=generic"; \
143
+ fi; \
144
+ SHARP_FORCE_GLOBAL_LIBVIPS=1 npm_config_nodedir=/usr/local \
145
+ NODE_PATH=/tmp/sharp-build/node_modules PATH="/tmp/sharp-build/node_modules/.bin:$PATH" \
146
+ node install/build.js; \
147
+ addon="sharp-linuxmusl-$(node -p "process.arch")-${version}.node"; \
148
+ test -f "src/build/Release/${addon}"; \
149
+ mkdir -p /opt/sharp-addon; \
150
+ cp "src/build/Release/${addon}" /opt/sharp-addon/; \
151
+ rm -rf src/build /tmp/sharp-build; \
152
+ mkdir -p src/build/Release; \
153
+ cp "/opt/sharp-addon/${addon}" src/build/Release/
154
+
32
155
  # ===================================================================
33
- # Stage 2: Build (compile Next.js)
156
+ # Stage 3: Build (compile Next.js)
34
157
  # ===================================================================
35
- FROM node:20-alpine@sha256:fb4cd12c85ee03686f6af5362a0b0d56d50c58a04632e6c0fb8363f609372293 AS builder
158
+ # Built FROM the dependencies stage: node_modules (with the CPU-safe addon)
159
+ # is already in place, and libvips is loadable should the build touch sharp.
160
+ # None of these build tools reach the runtime image.
161
+ FROM dependencies AS builder
36
162
 
37
163
  WORKDIR /app
38
164
 
39
- # Copy dependencies from previous stage
40
- COPY --from=dependencies /app/node_modules ./node_modules
41
-
42
- # Copy source code
165
+ # Copy source code (.dockerignore keeps node_modules out, so the installed
166
+ # modules above are not overwritten)
43
167
  COPY . .
44
168
 
45
169
  # Build arguments for env vars needed at build time
@@ -54,6 +178,19 @@ ENV NEXT_PUBLIC_APP_NAME=$NEXT_PUBLIC_APP_NAME
54
178
  # Disable Next.js telemetry during build
55
179
  ENV NEXT_TELEMETRY_DISABLED=1
56
180
 
181
+ # The API address must be known now. src/lib/env.ts falls back to
182
+ # http://localhost:8000/api/v1 when it is unset (handy for local builds and
183
+ # CI), so without this check an image built without the value builds green
184
+ # and every visitor's browser then calls its own machine: a client that looks
185
+ # deployed but cannot reach its API. A localhost value only warns, because a
186
+ # local docker test may mean it. (Whitespace-only counts as empty.)
187
+ RUN set -eu; \
188
+ url="$(printf '%s' "${NEXT_PUBLIC_API_BASE_URL:-}" | tr -d '[:space:]')"; \
189
+ case "$url" in \
190
+ "") echo "ERROR: NEXT_PUBLIC_API_BASE_URL is empty at build time. NEXT_PUBLIC_* values are baked into the client's JavaScript: in Coolify, set it on the client app and tick 'Available at Buildtime'." >&2; exit 1 ;; \
191
+ *://localhost*|*://127.0.0.1*) echo "WARNING: NEXT_PUBLIC_API_BASE_URL is $url: visitors' browsers would call their own machine. Fine for a local test, wrong for a deployed client." >&2 ;; \
192
+ esac
193
+
57
194
  # Build the application (outputs to .next/standalone)
58
195
  # NOTE: a `--mount=type=cache,target=/app/.next/cache` was tested here and REMOVED —
59
196
  # it measured SLOWER (next build 29s→58s on a real create-tigra client), because Next's
@@ -62,12 +199,29 @@ ENV NEXT_TELEMETRY_DISABLED=1
62
199
  RUN npm run build
63
200
 
64
201
  # ===================================================================
65
- # Stage 3: Production Runtime
202
+ # Stage 4: Production Runtime
66
203
  # ===================================================================
67
- FROM node:20-alpine@sha256:fb4cd12c85ee03686f6af5362a0b0d56d50c58a04632e6c0fb8363f609372293 AS production
204
+ FROM node:24-alpine@sha256:44fce696f38ba8c987c4db89954051000bbd05ffd8d8659ba15ed4a8602e937f AS production
205
+
206
+ # dumb-init: proper signal handling.
207
+ # curl: Coolify's own HTTP health check runs curl (then BusyBox wget) inside
208
+ # the container. Without curl it falls back to wget, which tries only the
209
+ # first address `localhost` resolves to (often IPv6 ::1, while Next listens
210
+ # on IPv4), so Coolify marks a healthy app unhealthy and rolls the deploy
211
+ # back. curl tries both addresses.
212
+ # The rest are the shared libraries our libvips links against (one per -dev
213
+ # package in stage 1; same list as the server image). Alpine ships WebP's
214
+ # mux/demux libraries separately from libwebp and sharp fails to load without
215
+ # them; the libheif plugins decode HEIC (libde265) and AVIF (aom).
216
+ RUN apk add --no-cache \
217
+ dumb-init curl \
218
+ glib expat libjpeg-turbo libpng libwebp libwebpmux libwebpdemux \
219
+ tiff lcms2 libheif libheif-libde265 libheif-aom libexif
68
220
 
69
- # Install dumb-init for proper signal handling
70
- RUN apk add --no-cache dumb-init
221
+ # The libvips built in stage 1. sharp's addon links to it without an rpath,
222
+ # so the dynamic loader is told where it lives.
223
+ COPY --from=libvips /opt/vips/lib /opt/vips/lib
224
+ ENV LD_LIBRARY_PATH=/opt/vips/lib
71
225
 
72
226
  # Create non-root user for security
73
227
  RUN addgroup -g 1001 -S nodejs && \
@@ -84,9 +238,22 @@ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
84
238
  # Copy public folder (favicon, images, etc.)
85
239
  COPY --from=builder --chown=nextjs:nodejs /app/public ./public
86
240
 
241
+ # Ship the CPU-safe sharp addon explicitly. Next's standalone file tracing
242
+ # does not reliably carry a source-built addon (or any libvips .so), so it is
243
+ # copied to the exact path sharp's loader tries first.
244
+ COPY --from=dependencies --chown=nextjs:nodejs /opt/sharp-addon/ ./node_modules/sharp/src/build/Release/
245
+
246
+ # Build-time sharp proof (see the script's header).
247
+ COPY --chown=nextjs:nodejs scripts/check-sharp.mjs ./scripts/check-sharp.mjs
248
+
87
249
  # Switch to non-root user
88
250
  USER nextjs
89
251
 
252
+ # Prove sharp loads our addon and converts images in THIS image, as the
253
+ # runtime user, before the image can ship. A failure fails the build instead
254
+ # of breaking every /_next/image request in production.
255
+ RUN node scripts/check-sharp.mjs
256
+
90
257
  # Disable telemetry at runtime
91
258
  ENV NEXT_TELEMETRY_DISABLED=1
92
259
 
@@ -97,9 +264,14 @@ ENV HOSTNAME="0.0.0.0"
97
264
  ENV PORT=3000
98
265
  EXPOSE 3000
99
266
 
100
- # Health check for container orchestration
267
+ # Health check for container orchestration. Coolify uses this one (not its
268
+ # dashboard check) to decide when a rolling deploy may switch over.
269
+ # /healthz (src/app/healthz/route.ts) answers 200 without rendering a page, so
270
+ # it stays healthy when a project makes its home page redirect; `/` did not.
271
+ # 127.0.0.1, never localhost: Next listens on IPv4 (HOSTNAME=0.0.0.0), and
272
+ # localhost can resolve to IPv6 ::1 first, which would refuse the probe.
101
273
  HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
102
- CMD node -e "const p=process.env.PORT||3000;require('http').get('http://localhost:'+p,(r)=>{process.exit(r.statusCode===200?0:1)})"
274
+ CMD node -e "const p=process.env.PORT||3000;require('http').get('http://127.0.0.1:'+p+'/healthz',(r)=>{process.exit(r.statusCode===200?0:1)})"
103
275
 
104
276
  # Use dumb-init to handle signals properly
105
277
  ENTRYPOINT ["dumb-init", "--"]
@@ -1,36 +1,107 @@
1
- This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
1
+ # Client
2
2
 
3
- ## Getting Started
3
+ The web app: Next.js 16 (App Router, Server Components by default), React 19, TypeScript, Tailwind CSS v4 and
4
+ shadcn/ui, React Query for server data, Redux Toolkit for the signed-in user, Vitest.
4
5
 
5
- First, run the development server:
6
+ It ships the wiring, not the screens: a welcome page, error and not-found pages, the `/healthz` health check, and
7
+ everything your own pages need to talk to the API safely.
8
+
9
+ ## Layout
10
+
11
+ ```text
12
+ client/src/
13
+ ├── app/ routes: page.tsx (welcome), layout.tsx, providers.tsx, error, not-found, healthz/
14
+ │ ├── (auth)/ a centred layout for your sign-in pages
15
+ │ └── (main)/ the layout with Header and Footer for the rest
16
+ ├── proxy.ts runs before every page: CSP nonce, security headers, protected-page redirect
17
+ ├── features/
18
+ │ ├── auth/ useAuth, useCurrentUser, AuthInitializer, LoginForm, RegisterForm,
19
+ │ │ ForgotPasswordForm, LimitNotice, usePasswordReset, auth.service
20
+ │ └── admin/ hooks and a service for stats, users and sessions
21
+ ├── components/ ui/ (shadcn), common/ (AppLink, SafeImage, ConfirmDialog, Pagination, …), layout/
22
+ ├── lib/
23
+ │ ├── api/ axios.config.ts (the API client), session-events.ts
24
+ │ ├── constants/ routes.ts (every page path, PROTECTED_PATHS), api-endpoints.ts
25
+ │ └── utils/ error and field-error parsing, safe redirects, paths, the session marker, forms
26
+ ├── store/ makeStore(): one Redux store per visitor
27
+ └── styles/ theme tokens (light and dark) and fonts
28
+ ```
29
+
30
+ ## Commands
6
31
 
7
32
  ```bash
8
- npm run dev
9
- # or
10
- yarn dev
11
- # or
12
- pnpm dev
13
- # or
14
- bun dev
33
+ npm run dev # http://localhost:3000, with fast refresh
34
+ npm test # unit tests
35
+ npm run test:coverage # with the coverage floor (vitest.config.mts)
36
+ npm run lint && npm run typecheck
37
+ npm run build # production build (standalone output, what the Docker image runs)
38
+ ```
39
+
40
+ `NEXT_PUBLIC_API_BASE_URL` (in `.env`, written when the project was created) is the API address, for example
41
+ `http://localhost:8000/api/v1`. Every `NEXT_PUBLIC_*` value is public and is baked into the build.
42
+
43
+ ## Adding your pages
44
+
45
+ Put them at the paths in `src/lib/constants/routes.ts`; the proxy, the API client and the server's emails already
46
+ send people there.
47
+
48
+ ```tsx
49
+ // src/app/(auth)/login/page.tsx
50
+ import { LoginForm } from '@/features/auth/components/LoginForm';
51
+
52
+ export default function LoginPage() {
53
+ return <LoginForm />;
54
+ }
15
55
  ```
16
56
 
17
- Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
57
+ | Path | Page | Use |
58
+ |---|---|---|
59
+ | `/login` | sign in | `LoginForm`: reads `?from=` and returns there, but only to a page of this site |
60
+ | `/register` | sign up | `RegisterForm` |
61
+ | your choice | forgot password | `ForgotPasswordForm` |
62
+ | `/reset-password` | new password from the emailed `?token=` | `useResetPassword` |
63
+ | `/verify-account` | email verification (email module) | `useVerifyAccount`, `useSendVerification` |
64
+ | `/dashboard`, `/profile`, `/admin`, and below | signed-in areas | `useCurrentUser`, `useAuth`, the admin hooks |
65
+
66
+ The forms already show the server's "slow down" answers with the wait time (`LimitNotice`) and its field errors
67
+ under their fields (`applyFieldErrors`). Building your own form on `useAuth`? Do the same, as `LoginForm` does.
18
68
 
19
- You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
69
+ Pages that need a signed-in visitor are listed once, in `PROTECTED_PATHS` (`routes.ts`), and matched by whole path
70
+ segments: `/admin` covers `/admin/users`, never `/administrator`. Add your own areas there. The proxy only checks
71
+ that a session marker exists; the API decides what each user may see.
72
+
73
+ ## How sign-in works
74
+
75
+ ```text
76
+ sign in the API sets httpOnly cookies: access_token, refresh_token, and auth_session (a non-secret marker)
77
+ start-up AuthInitializer asks "who am I" (GET /auth/me), only when the auth_session marker is present;
78
+ anonymous visitors make no API calls at all
79
+ 401 the API client refreshes once (parallel requests wait for that one refresh), then replays the request
80
+ lost race another tab refreshed first (401 REFRESH_TOKEN_ROTATED): its fresh cookies are already in the
81
+ browser, so the request is replayed and nobody is signed out
82
+ dead session the refresh is refused: the user is signed out and sent to /login
83
+ banned a call answers 403 ACCOUNT_DEACTIVATED: the client signs out through the logout route at once
84
+ ```
20
85
 
21
- This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Inter](https://fonts.google.com/specimen/Inter) and [JetBrains Mono](https://fonts.google.com/specimen/JetBrains+Mono). Fonts are managed via the font preset system in `src/styles/fonts/` — see the design system rules for how to switch fonts.
86
+ Tokens never reach JavaScript. The signed-in user lives in Redux (`state.auth`); read it with `useAppSelector` or
87
+ `useAuth`. Server data belongs in React Query, page data in Server Components.
22
88
 
23
- ## Learn More
89
+ ## Security headers
24
90
 
25
- To learn more about Next.js, take a look at the following resources:
91
+ `src/proxy.ts` (Next 16's name for middleware) is the one place for them. Each request gets a fresh CSP nonce with
92
+ `strict-dynamic`; Next stamps it on its own scripts. Do not add a second CSP in `next.config.ts`. A script you add
93
+ inline needs the nonce from the `x-nonce` header (see `app/layout.tsx`).
26
94
 
27
- - [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
28
- - [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
95
+ ## Tests
29
96
 
30
- You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!
97
+ Unit tests sit next to the code in `__tests__/` folders: the API client's session rules, the proxy, redirect safety,
98
+ paths, the session marker, error parsing, the store. Keep logic in functions under `src/lib/` and test it there; a
99
+ test that needs `document` or `window` starts with `// @vitest-environment jsdom`. CI runs `npm run test:coverage`.
31
100
 
32
- ## Deploy on Vercel
101
+ ## Deploying
33
102
 
34
- The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
103
+ The `Dockerfile` builds the standalone server and runs it as a non-root user, with a health check on `/healthz`.
104
+ On Coolify, set `NEXT_PUBLIC_API_BASE_URL` with "Available at Buildtime" ticked: the build stops without it. The
105
+ full checklist is in the server's `docs/deployment.md` and the create-tigra README.
35
106
 
36
- Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
107
+ `AGENTS.md` in this folder has the rules for changing this code.
@@ -2,7 +2,7 @@ import type { NextConfig } from "next";
2
2
 
3
3
  // SECURITY HEADERS / CSP NOTE:
4
4
  // All security headers — including the Content-Security-Policy — are set in
5
- // src/middleware.ts, which is the SINGLE SOURCE OF TRUTH for them. The CSP is
5
+ // src/proxy.ts, which is the SINGLE SOURCE OF TRUTH for them. The CSP is
6
6
  // per-request and nonce-based ('strict-dynamic'), which a static headers() block
7
7
  // here cannot express, and emitting a CSP from both places would produce a
8
8
  // conflicting duplicate Content-Security-Policy header. Do NOT add a headers()