create-tigra 4.0.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.
- package/CHANGELOG.md +151 -0
- package/README.md +221 -82
- package/bin/create-tigra.js +2 -0
- package/package.json +3 -2
- package/template/README.md +79 -0
- package/template/client/README.md +92 -21
- package/template/server/README.md +108 -0
- package/template/server/docs/logging.md +16 -2
- package/template/server/docs/testing.md +20 -2
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
A generated project never changes after it is created: these notes say what new projects get. To bring an older
|
|
4
|
+
project along, port the changes you need; the guides in [docs/](docs/) cover the ones that need care.
|
|
5
|
+
|
|
6
|
+
## 4.0.1 (2026-10-20)
|
|
7
|
+
|
|
8
|
+
Documentation only, no code changes.
|
|
9
|
+
|
|
10
|
+
- The README explains what create-tigra builds, area by area, how a request travels, the pages you add yourself,
|
|
11
|
+
the everyday commands, `create-tigra add`, and where every guide lives.
|
|
12
|
+
- Generated projects get a root `README.md`, a `server/README.md` and a rewritten `client/README.md` (it was
|
|
13
|
+
create-next-app's default text).
|
|
14
|
+
- `server/docs/testing.md` covers the client tests and the route-contract rule; `server/docs/logging.md` covers
|
|
15
|
+
secret redaction.
|
|
16
|
+
- This changelog, now shipped in the npm package.
|
|
17
|
+
- The CLI fills the project's name, database and local ports into its new README.
|
|
18
|
+
|
|
19
|
+
## 4.0.0 (2026-10-20)
|
|
20
|
+
|
|
21
|
+
A full review of the template, area by area. Every change was tested on fresh projects with and without the
|
|
22
|
+
email-verification module, and the Docker images were built and run on Coolify.
|
|
23
|
+
|
|
24
|
+
### Breaking for new projects
|
|
25
|
+
|
|
26
|
+
- Generated apps need **Node 24 and npm 11.19+** (the 3.3.0 images ran Node 20). The images use `node:24-alpine`
|
|
27
|
+
pinned by digest, and the tested lockfiles ship with every project and install with `npm ci`.
|
|
28
|
+
- Local and test MySQL is **8.4**.
|
|
29
|
+
- In production the server refuses to start without any migration, without `REDIS_URL`, or when Redis cannot be
|
|
30
|
+
reached within 10 s of starting.
|
|
31
|
+
- `TRUST_CLOUDFLARE`, `DATABASE_POOL_MIN` and `DATABASE_POOL_MAX` are gone; `CLOUDFLARE_ORIGIN_SECRET` is new.
|
|
32
|
+
- Wrong input always answers `422 VALIDATION_FAILED` with the list of fields.
|
|
33
|
+
- The public `/api/v1/health` answers only its status; the full report moved to `GET /api/v1/admin/health`.
|
|
34
|
+
- Client: `src/middleware.ts` is now `src/proxy.ts` (Next 16's name), and the Redux store is created per visitor
|
|
35
|
+
with `makeStore()` instead of being one exported `store`.
|
|
36
|
+
- `create-tigra add email-verification` is meant for 4.x projects: it copies tests that use the 4.x test helpers.
|
|
37
|
+
|
|
38
|
+
### Generator
|
|
39
|
+
|
|
40
|
+
- New projects ship their first migration (`0_init`); a check keeps it equal to the schema.
|
|
41
|
+
- The README no longer tells you to copy `.env.example` over the `.env` that holds the generated secrets.
|
|
42
|
+
- The CLI warns when Node is older than 24 or npm older than 11.19.
|
|
43
|
+
|
|
44
|
+
### Foundation and configuration
|
|
45
|
+
|
|
46
|
+
- `REDIS_URL` is required in production; on/off switches accept only `true` or `false`.
|
|
47
|
+
- `CLIENT_URL` is required in production only when emails can go out.
|
|
48
|
+
- Email verification is off unless the email module is installed.
|
|
49
|
+
- The production example uses `PORT=8000` and caps the database pool (`?connection_limit=5`).
|
|
50
|
+
|
|
51
|
+
### Request pipeline
|
|
52
|
+
|
|
53
|
+
- The client IP is verified before it is believed: behind Cloudflare, behind Coolify's Traefik, or direct
|
|
54
|
+
([guide](docs/client-ip-behind-proxies.md)).
|
|
55
|
+
- Rate limits for public use: clear wait times (`retryAfterSeconds`), a per-address email cap, overrides through
|
|
56
|
+
env (`RATE_LIMIT_OVERRIDES`, `RATE_LIMIT_MULTIPLIER`); token flows answer a clear 503 while Redis is down.
|
|
57
|
+
- `CORS_ORIGIN` is checked at boot (bare origins only, with the fix printed).
|
|
58
|
+
- Auth cookies are never `SameSite=None`: the tokens are Strict and the session marker is Lax, so a link from
|
|
59
|
+
another site no longer bounces a signed-in user to `/login`.
|
|
60
|
+
- A warning when production runs on plain http (the browser would drop the login cookies).
|
|
61
|
+
- Log redaction covers secrets at every level it promises; an inbound `X-Request-Id` is kept only when short and
|
|
62
|
+
plain.
|
|
63
|
+
|
|
64
|
+
### Sessions and accounts
|
|
65
|
+
|
|
66
|
+
- Two tabs refreshing at once: exactly one wins and the other keeps its session.
|
|
67
|
+
- Refresh, reset and verification tokens are stored as SHA-256 fingerprints only.
|
|
68
|
+
- Signing out, signing out everywhere, a password reset and an admin revoke end the access token at once; a banned
|
|
69
|
+
user's token answers `403 ACCOUNT_DEACTIVATED` at once.
|
|
70
|
+
- A password-reset link works once, even when used twice at the same moment.
|
|
71
|
+
- A wrong password on change-password or delete-account answers 422, not 401; changing your own password keeps
|
|
72
|
+
this device signed in.
|
|
73
|
+
- Strangers can no longer use up a user's verification emails.
|
|
74
|
+
|
|
75
|
+
### Users and files
|
|
76
|
+
|
|
77
|
+
- Multipart fields are capped, so one upload cannot hold about 1 GB of memory; a file cut at the size limit
|
|
78
|
+
answers 413.
|
|
79
|
+
- Avatar formats are judged by their bytes (JPEG, PNG, WebP, HEIC, AVIF); HEIC is proven to decode in the image
|
|
80
|
+
build. Every avatar gets a new file name, and the old file goes only after the swap.
|
|
81
|
+
- A deleted account's photo stops being public at once; names are trimmed before their length is checked.
|
|
82
|
+
- The server warns at boot when uploads are not on a persistent volume.
|
|
83
|
+
|
|
84
|
+
### Admin API
|
|
85
|
+
|
|
86
|
+
- An admin cannot ban themselves, and the last admin cannot delete their own account.
|
|
87
|
+
- IP blocks use one canonical spelling, match however they were written, and cannot block an admin's own or a
|
|
88
|
+
private address.
|
|
89
|
+
- Deleted accounts can be listed and restored during their 30 days.
|
|
90
|
+
- `make-admin` promotes an existing account to admin (the first admin, or recovery).
|
|
91
|
+
- User detail says `lastActiveAt`; the user list's filters work again.
|
|
92
|
+
|
|
93
|
+
### Background jobs
|
|
94
|
+
|
|
95
|
+
- Jobs run about a minute after every start, then on their schedule, so frequent deploys never postpone them
|
|
96
|
+
(before, an app deployed at least daily never purged deleted accounts).
|
|
97
|
+
- Expired automatic IP blocks are pruned hourly; an account another process already purged is not a failure.
|
|
98
|
+
|
|
99
|
+
### Outbound requests and email
|
|
100
|
+
|
|
101
|
+
- `safeHttpClient` for URLs a user supplied: it refuses internal addresses on every redirect hop. The
|
|
102
|
+
internal-address check no longer misses hex IPv4-mapped forms.
|
|
103
|
+
- Outbound URLs are logged redacted; failures carry their status and kind.
|
|
104
|
+
- Sending email gives up after 10 s, and recipients are logged masked. Production refuses the placeholder Resend
|
|
105
|
+
key and sender.
|
|
106
|
+
|
|
107
|
+
### Health, logs and shutdown
|
|
108
|
+
|
|
109
|
+
- Memory is judged against the heap limit; a failing check logs the real error.
|
|
110
|
+
- The database and Redis are checked at most once per 5 s for all probes together.
|
|
111
|
+
- Request lines carry JSON fields in production and never keep the query string.
|
|
112
|
+
- Crashes and failed starts exit 1, and shutdown has an 8 s deadline (inside Docker's 10 s).
|
|
113
|
+
|
|
114
|
+
### Testing and CI
|
|
115
|
+
|
|
116
|
+
- The critical advisories in the shipped dependencies are cleared.
|
|
117
|
+
- Integration tests ship with every project (166, or 172 with the email module): every route's contract, plus identity, users, admin, jobs, health
|
|
118
|
+
and email behaviour, against a real MySQL and Redis (Testcontainers, or services you already run). The route
|
|
119
|
+
contract fails when a new route is never called.
|
|
120
|
+
- An honest coverage floor counts every file, on the server and now on the client.
|
|
121
|
+
- The server's lint refuses unawaited and misused promises.
|
|
122
|
+
- New projects ship CI, a weekly audit of production dependencies and Dependabot (security updates for npm and
|
|
123
|
+
Actions, monthly base-image updates).
|
|
124
|
+
|
|
125
|
+
### Deployment
|
|
126
|
+
|
|
127
|
+
- The images run on any x86-64 CPU: sharp is built on a pinned libvips, so VPSes without x86-64-v2 work.
|
|
128
|
+
- curl health checks; plain (uncoloured) production logs.
|
|
129
|
+
- The client image build stops when `NEXT_PUBLIC_API_BASE_URL` is missing; the client health check is `/healthz`,
|
|
130
|
+
which works even when the home page redirects.
|
|
131
|
+
- A Coolify checklist in the README and `server/docs/deployment.md`: what a deploy does, safe migrations (expand,
|
|
132
|
+
deploy, contract), rollback, and the libvips build time.
|
|
133
|
+
|
|
134
|
+
### Guidance for AI agents
|
|
135
|
+
|
|
136
|
+
- The completion check reviews the patch actually handed over (`git diff HEAD` plus new files). Claude Code's
|
|
137
|
+
`/security-review` is not that check: it only sees commits.
|
|
138
|
+
- The root `AGENTS.md` tells Codex to read the server and client files, which it does not load by itself.
|
|
139
|
+
- The agent files now carry the migration, job, deployment and test rules, and the security workflow covers the
|
|
140
|
+
client's security code.
|
|
141
|
+
|
|
142
|
+
### Client
|
|
143
|
+
|
|
144
|
+
- A Vitest unit suite (session rules, the proxy, redirect safety, error parsing) runs in CI with a coverage floor.
|
|
145
|
+
- A refresh race lost to another tab is replayed instead of signing out; a ban mid-session signs out at once.
|
|
146
|
+
- Anonymous visitors make no API calls (it was two failing calls per page view).
|
|
147
|
+
- Protected paths match whole segments (`/administrator` is no longer treated as `/admin`).
|
|
148
|
+
- The server's field errors show under their fields; the register rules mirror the server's.
|
|
149
|
+
- The header is right on the first render, and hydration uses the server's state (a second tab could hit a
|
|
150
|
+
hydration mismatch).
|
|
151
|
+
- The welcome page links to this repository.
|
package/README.md
CHANGED
|
@@ -1,108 +1,191 @@
|
|
|
1
1
|
# create-tigra
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`create-tigra` generates a full-stack TypeScript app that is ready for production on day one: a **Fastify 5** API
|
|
4
|
+
with **Prisma** (MySQL 8.4) and **Redis 7**, and a **Next.js 16** client. Sign-in and sessions, abuse protection,
|
|
5
|
+
uploads, an admin API, background jobs, health checks, tests, CI and Docker images for Coolify are already built,
|
|
6
|
+
tested and documented, so a new project starts at its own features instead of its plumbing.
|
|
4
7
|
|
|
5
|
-
|
|
8
|
+
The client ships the wiring, not the screens. Every app looks different, so you write the pages; the template gives
|
|
9
|
+
them the API client, the sign-in flow, the forms and the security headers they need.
|
|
6
10
|
|
|
7
11
|
```bash
|
|
8
12
|
npx create-tigra my-app
|
|
9
13
|
```
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
The CLI asks for a project name and whether new users must verify their email, then writes the project with fresh
|
|
16
|
+
secrets in its git-ignored `.env` files.
|
|
17
|
+
|
|
18
|
+
- [What you get](#what-you-get)
|
|
19
|
+
- [Quick start](#quick-start)
|
|
20
|
+
- [Everyday commands](#everyday-commands)
|
|
21
|
+
- [Testing and CI](#testing-and-ci)
|
|
22
|
+
- [Deploying to Coolify](#deploying-to-coolify)
|
|
23
|
+
- [Adding a module later](#adding-a-module-later)
|
|
24
|
+
- [Documentation](#documentation)
|
|
25
|
+
- [Upgrading from 3.x](#upgrading-from-3x)
|
|
26
|
+
|
|
27
|
+
## What you get
|
|
28
|
+
|
|
29
|
+
```text
|
|
30
|
+
my-app/
|
|
31
|
+
├── server/ Fastify 5 API: TypeScript, Prisma, MySQL 8.4, Redis 7, Vitest, Dockerfile
|
|
32
|
+
├── client/ Next.js 16 app: App Router, React 19, Tailwind v4, shadcn/ui, Vitest, Dockerfile
|
|
33
|
+
├── .github/ CI on every push, a weekly dependency audit, Dependabot
|
|
34
|
+
├── AGENTS.md rules for AI coding agents (one more in server/ and client/), CLAUDE.md imports them
|
|
35
|
+
└── .agents/ the security-audit skill the agents use before they finish a change
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
How a request travels:
|
|
39
|
+
|
|
40
|
+
```text
|
|
41
|
+
Browser
|
|
42
|
+
│ client/src/proxy.ts: a fresh CSP nonce per request, security headers,
|
|
43
|
+
│ signed-out visitors sent away from protected pages
|
|
44
|
+
▼
|
|
45
|
+
Next.js client ── axios, httpOnly cookies ──▶ Fastify API /api/v1/...
|
|
46
|
+
one refresh per tab; a refresh race │ CORS allowlist + origin check, real visitor IP,
|
|
47
|
+
between tabs is replayed, not a sign-out │ IP blocks, per-route rate limits, Zod input checks
|
|
48
|
+
▼
|
|
49
|
+
route → controller → service → repository → Prisma → MySQL
|
|
50
|
+
│ Redis: rate limits, lockout, IP blocks
|
|
51
|
+
└─ background jobs · Resend email · health probes
|
|
52
|
+
```
|
|
12
53
|
|
|
13
|
-
###
|
|
54
|
+
### Server
|
|
14
55
|
|
|
15
|
-
|
|
|
56
|
+
| Area | What is built |
|
|
16
57
|
|---|---|
|
|
17
|
-
|
|
|
18
|
-
|
|
|
19
|
-
|
|
|
20
|
-
|
|
|
21
|
-
|
|
|
22
|
-
|
|
|
23
|
-
|
|
|
24
|
-
|
|
|
25
|
-
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
|
58
|
+
| Accounts and sign-in | Register, sign in, sign out, sign out everywhere, the list of your sessions, forgot and reset password by email. Tokens live in httpOnly cookies (access 15 min, refresh 7 days by default); passwords are hashed with Argon2id. |
|
|
59
|
+
| Sessions | Every refresh rotates the token. When two tabs refresh at once, exactly one wins and the other keeps its session. Tokens are stored only as SHA-256 fingerprints. Signing out, a password reset, a ban or an admin revoke ends the access token at once, not 15 minutes later. |
|
|
60
|
+
| Abuse protection | Rate limits per route and per IP, tuned from env (`RATE_LIMIT_MULTIPLIER`, `RATE_LIMIT_OVERRIDES`). A login lockout per email and IP (the 15th failure locks for 15 min, the 30th for 1 h) that a stranger cannot use against the real user. An automatic IP block for repeat offenders, a manual block list, and a per-address cap on emails anyone can trigger. Every "slow down" answer says how long to wait. |
|
|
61
|
+
| Real visitor address | Verified behind Cloudflare and Coolify's Traefik before it is believed, so nobody can pick their own rate-limit key or get someone else blocked. Check it any time at `GET /api/v1/admin/client-ip`. |
|
|
62
|
+
| Input and errors | Zod validates every input. Wrong input always answers `422 VALIDATION_FAILED` with the list of fields (never the submitted values); an unexpected error answers a generic 500 and is logged in full on the server. |
|
|
63
|
+
| Users and files | Profile, password change, account deletion (30 days to change your mind, then purged). Avatars are judged by their bytes (JPEG, PNG, WebP, HEIC, AVIF), stored as WebP with location data stripped, capped at 10 MB and 50 megapixels. Private files are served only to their owner. |
|
|
64
|
+
| Admin API | Stats, the user list (search, role, status, deleted accounts), user detail, ban and unban, role change, restoring a deleted account, the session list and revoke, IP blocks. An admin cannot ban themselves and the last admin cannot delete their account. The first admin is promoted with `make-admin`. |
|
|
65
|
+
| Outbound requests | One shared HTTP client with timeouts and redacted logs, plus `safeHttpClient` for URLs a user supplied: it refuses internal addresses (localhost, private networks, cloud metadata) on every redirect hop. |
|
|
66
|
+
| Email | Resend, with a 10 s timeout and masked addresses in the logs. Optional email verification for new users. |
|
|
67
|
+
| Background jobs | Run inside the server process, no extra service: expired sessions and tokens hourly, deleted accounts past 30 days daily, Cloudflare's IP list daily. Each first runs about a minute after the server starts, so frequent deploys never postpone them. |
|
|
68
|
+
| Health and logs | `/api/v1/live`, `/ready` and `/health` for probes, and the full report for admins at `/api/v1/admin/health`. One log line per request (JSON fields in production, never the query string) with passwords, tokens, cookies and keys redacted. A crash exits 1; shutdown finishes in-flight requests within 8 s. |
|
|
69
|
+
| Database and Redis | MySQL 8.4 with Prisma 6. New projects ship their first migration; production applies migrations at start and refuses to start without one. Production also refuses to start without Redis, because the limits, the lockout and the IP blocks depend on it. |
|
|
70
|
+
| Local infrastructure | Docker Compose for MySQL and Redis, reachable from your machine only; phpMyAdmin and Redis Commander when you ask for them. Each project gets its own ports, so several can run side by side. |
|
|
71
|
+
|
|
72
|
+
### Client
|
|
73
|
+
|
|
74
|
+
| Area | What is built |
|
|
30
75
|
|---|---|
|
|
31
|
-
| Framework |
|
|
32
|
-
|
|
|
33
|
-
|
|
|
34
|
-
|
|
|
35
|
-
|
|
|
36
|
-
|
|
|
37
|
-
|
|
|
38
|
-
|
|
|
39
|
-
| Testing | Vitest
|
|
40
|
-
|
|
41
|
-
|
|
76
|
+
| Framework | Next.js 16 (App Router, Server Components by default), React 19, TypeScript strict. |
|
|
77
|
+
| Security | `src/proxy.ts` (Next 16's name for middleware): a fresh CSP nonce per request with `strict-dynamic`, security headers, and a redirect to sign-in for protected pages. Tokens never reach JavaScript. |
|
|
78
|
+
| API client | Axios with httpOnly cookies. Parallel 401s share one refresh; a refresh race lost to another tab is replayed instead of signing out; a ban mid-session signs out at once; "slow down" answers show one toast with the wait time. |
|
|
79
|
+
| Sign-in wiring | `useAuth`, `useCurrentUser`, `AuthInitializer`, and the login, register and forgot-password form components: inline limit messages, the server's field errors under their fields, and a `?from=` redirect that only ever lands on your own site. Anonymous visitors make no API calls. |
|
|
80
|
+
| Admin wiring | Hooks and services for stats, users and sessions, ready for your own admin screens. |
|
|
81
|
+
| State | Redux Toolkit only for the signed-in user (one store per visitor), React Query for server data. |
|
|
82
|
+
| Design system | Tailwind CSS v4, shadcn/ui, OKLCH colour tokens with light and dark themes (next-themes). |
|
|
83
|
+
| Components | Header, footer, layouts, links with smooth navigation, a safe image, confirm dialog, empty state, pagination, loading states, error and not-found pages. |
|
|
84
|
+
| Testing | Vitest unit tests of the client's logic (session rules, the proxy, redirect safety, error parsing) with a coverage floor in CI. |
|
|
85
|
+
|
|
86
|
+
### Pages are yours
|
|
87
|
+
|
|
88
|
+
Besides a welcome page and the `/healthz` health check, the client ships no pages, on purpose. Add yours at the
|
|
89
|
+
paths in `client/src/lib/constants/routes.ts`; the proxy, the API client and the emails already send people there:
|
|
90
|
+
|
|
91
|
+
| Path | Your page | What to use |
|
|
92
|
+
|---|---|---|
|
|
93
|
+
| `/login` | sign in | `LoginForm` (reads `?from=`) |
|
|
94
|
+
| `/register` | sign up | `RegisterForm` |
|
|
95
|
+
| any path you like | forgot password | `ForgotPasswordForm` |
|
|
96
|
+
| `/reset-password?token=…` | new password, from the emailed link | `useResetPassword` |
|
|
97
|
+
| `/verify-account?token=…` | email verification (email module) | `useVerifyAccount`, `useSendVerification` |
|
|
98
|
+
| `/dashboard`, `/profile`, `/admin` and below | signed-in areas, protected by the proxy | `useCurrentUser`, the admin hooks |
|
|
42
99
|
|
|
43
|
-
|
|
100
|
+
The protected list lives next to the routes (`PROTECTED_PATHS`); add your own areas there.
|
|
44
101
|
|
|
45
|
-
|
|
102
|
+
### Built for AI agents
|
|
46
103
|
|
|
47
|
-
|
|
104
|
+
- `AGENTS.md` at the root and in `server/` and `client/`, with `CLAUDE.md` files that import them, so Codex and
|
|
105
|
+
Claude Code read the same rules: the architecture, the sources of truth, safe migrations, deployment and tests.
|
|
106
|
+
- Cloudflare's MIT-licensed `security-audit` skill (`.agents/skills/`, with a pointer in `.claude/skills/`). Before
|
|
107
|
+
finishing a meaningful change, the agent reviews the patch it is handing over (`git diff HEAD` plus new files):
|
|
108
|
+
with the Codex Security plugin's diff scan when it is installed, otherwise with this skill. Full audits stay an
|
|
109
|
+
explicit request.
|
|
110
|
+
- The code itself is the guide: one layout per module, one response and error shape, one rate-limit config, and a
|
|
111
|
+
route-contract test that fails when a new route has no test.
|
|
48
112
|
|
|
49
|
-
##
|
|
113
|
+
## Quick start
|
|
50
114
|
|
|
51
|
-
|
|
52
|
-
- **Docker** (for MySQL and Redis)
|
|
115
|
+
Prerequisites:
|
|
53
116
|
|
|
54
|
-
|
|
117
|
+
- **Node.js 24+** with **npm 11.19+** for the generated apps (the CLI itself runs on Node 22.12+). Early Node 24
|
|
118
|
+
releases ship an older npm 11 whose lockfiles the Docker images reject: check `npm -v`, update with
|
|
119
|
+
`npm install -g npm@11`.
|
|
120
|
+
- **Docker**, for the local MySQL and Redis.
|
|
55
121
|
|
|
56
122
|
```bash
|
|
57
|
-
|
|
58
|
-
cd my-app/server
|
|
59
|
-
docker compose up -d
|
|
123
|
+
npx create-tigra my-app
|
|
60
124
|
|
|
61
|
-
#
|
|
62
|
-
|
|
63
|
-
#
|
|
64
|
-
|
|
65
|
-
npm ci
|
|
125
|
+
# Server: infrastructure, database, API
|
|
126
|
+
cd my-app/server
|
|
127
|
+
npm ci # installs the shipped lockfile exactly
|
|
128
|
+
npm run docker:up # MySQL and Redis for this project
|
|
66
129
|
npm run prisma:generate
|
|
67
|
-
npm run prisma:migrate:dev # creates the tables from the shipped
|
|
130
|
+
npm run prisma:migrate:dev # creates the tables from the shipped first migration
|
|
131
|
+
npm run dev # http://localhost:8000
|
|
68
132
|
|
|
69
|
-
#
|
|
70
|
-
npm run dev
|
|
71
|
-
|
|
72
|
-
# 4. In a new terminal, set up the client
|
|
133
|
+
# Client, in a second terminal
|
|
73
134
|
cd my-app/client
|
|
74
135
|
npm ci
|
|
75
|
-
npm run dev
|
|
136
|
+
npm run dev # http://localhost:3000
|
|
76
137
|
```
|
|
77
138
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
- phpMyAdmin and Redis Commander (optional, see below): each project gets its own ports, so several projects can run side by side. The CLI prints them when it finishes; they are also in `server/docker-compose.yml`.
|
|
139
|
+
The CLI already wrote `server/.env` and `client/.env` with fresh secrets. Do not copy `.env.example` over them: the
|
|
140
|
+
server refuses the example's placeholder secret.
|
|
81
141
|
|
|
82
|
-
The admin
|
|
142
|
+
The database admin tools are off by default. `npm run docker:tools` starts phpMyAdmin and Redis Commander on the
|
|
143
|
+
ports the CLI printed (also in `server/docker-compose.yml`); `npm run docker:down` stops everything.
|
|
83
144
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
145
|
+
## Everyday commands
|
|
146
|
+
|
|
147
|
+
| In `server/` | What it does |
|
|
148
|
+
|---|---|
|
|
149
|
+
| `npm run dev` | API with reload on save |
|
|
150
|
+
| `npm test` / `npm run test:coverage` | unit tests (no database); with the coverage floor |
|
|
151
|
+
| `npm run test:integration` | every route against a real MySQL and Redis |
|
|
152
|
+
| `npm run lint` / `npm run typecheck` | ESLint / TypeScript |
|
|
153
|
+
| `npm run build` / `npm start` | production build / run it |
|
|
154
|
+
| `npm run prisma:migrate:dev` | create and apply a migration after a schema change |
|
|
155
|
+
| `npm run prisma:studio` | browse the database |
|
|
156
|
+
| `npm run make-admin -- you@example.com` | promote an existing account to admin |
|
|
157
|
+
| `npm run docker:up` / `docker:tools` / `docker:down` | local MySQL and Redis / plus the admin UIs / stop |
|
|
158
|
+
|
|
159
|
+
| In `client/` | What it does |
|
|
160
|
+
|---|---|
|
|
161
|
+
| `npm run dev` | the app with fast refresh |
|
|
162
|
+
| `npm test` / `npm run test:coverage` | unit tests / with the coverage floor |
|
|
163
|
+
| `npm run lint` / `npm run typecheck` | ESLint / TypeScript |
|
|
164
|
+
| `npm run build` / `npm start` | production build / run it locally (the Docker image runs the standalone `server.js`) |
|
|
88
165
|
|
|
89
166
|
## Testing and CI
|
|
90
167
|
|
|
91
168
|
```bash
|
|
92
169
|
cd my-app/server
|
|
93
170
|
npm test # unit tests: no database, no Docker
|
|
94
|
-
npm run test:coverage # the same,
|
|
171
|
+
npm run test:coverage # the same, failing below the coverage floor
|
|
95
172
|
npm run test:integration # every route against a real MySQL + Redis
|
|
173
|
+
|
|
174
|
+
cd ../client
|
|
175
|
+
npm test # the client's logic: session rules, proxy, redirects, error parsing
|
|
176
|
+
npm run test:coverage
|
|
96
177
|
```
|
|
97
178
|
|
|
98
179
|
The integration tests start a throwaway MySQL and Redis with Testcontainers (Docker must run). To use services you
|
|
99
180
|
already run instead, set `INTEGRATION_DATABASE_URL` (a database whose name ends in `_test`) and
|
|
100
|
-
`INTEGRATION_REDIS_URL` (a Redis database 1
|
|
181
|
+
`INTEGRATION_REDIS_URL` (a Redis database from 1 to 15): the suite empties both first. Every route has a contract
|
|
182
|
+
test (who may call it, wrong input, success), and that file fails when a new route is never called. Details:
|
|
101
183
|
`server/docs/testing.md`.
|
|
102
184
|
|
|
103
|
-
New projects ship
|
|
104
|
-
|
|
105
|
-
|
|
185
|
+
New projects ship GitHub workflows in `.github/`:
|
|
186
|
+
|
|
187
|
+
- `ci.yml`, on every push and pull request: typecheck, lint, unit tests with coverage and build for both apps, and
|
|
188
|
+
the server's integration tests (Testcontainers on GitHub's runners). It never deploys.
|
|
106
189
|
- `audit.yml`, weekly: `npm audit` of what production installs, red on a critical advisory. It does not block merges.
|
|
107
190
|
- `dependabot.yml`: security-update pull requests for npm packages and Actions (turn them on once in the
|
|
108
191
|
repository's Settings → Advanced Security → Dependabot security updates), and a monthly pull request that
|
|
@@ -110,7 +193,8 @@ New projects ship with GitHub workflows in `.github/`:
|
|
|
110
193
|
|
|
111
194
|
## Deploying to Coolify
|
|
112
195
|
|
|
113
|
-
Server and client deploy as two separate Coolify applications, each from its own Dockerfile (`server/Dockerfile`,
|
|
196
|
+
Server and client deploy as two separate Coolify applications, each from its own Dockerfile (`server/Dockerfile`,
|
|
197
|
+
`client/Dockerfile`).
|
|
114
198
|
|
|
115
199
|
**Coolify checklist.** One Coolify project with two applications (from the same Git repository), a MySQL and a Redis:
|
|
116
200
|
|
|
@@ -125,13 +209,18 @@ Server and client deploy as two separate Coolify applications, each from its own
|
|
|
125
209
|
| Storage | a volume at `/app/uploads` (below) | none |
|
|
126
210
|
|
|
127
211
|
How the domains map to the variables:
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
212
|
+
|
|
213
|
+
```text
|
|
214
|
+
NEXT_PUBLIC_API_BASE_URL (client) https://api.example.com/api/v1 the API domain + /api/v1
|
|
215
|
+
CORS_ORIGIN (server) https://example.com the site's origin exactly: no path, no trailing slash
|
|
216
|
+
CLIENT_URL (server) https://example.com used in the links of emails
|
|
217
|
+
COOKIE_DOMAIN (server) .example.com when site and API are sibling subdomains; unset on one host
|
|
218
|
+
DATABASE_URL (server) mysql://…internal…/app?connection_limit=5
|
|
219
|
+
REDIS_URL (server) redis://…internal… the internal URLs from Coolify's MySQL and Redis pages
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
A wrong database URL fails the deploy at its migration step, and production refuses to start when it cannot reach
|
|
223
|
+
Redis within 10 s. Either way the old container keeps serving.
|
|
135
224
|
|
|
136
225
|
Deploy the server first (it applies the migrations), then the client. Afterwards sign in as an admin and open
|
|
137
226
|
`/api/v1/admin/health`: every check should be up. Coolify switches traffic to a new container only once its Dockerfile
|
|
@@ -140,22 +229,72 @@ container still serves; before one that drops or renames anything, read `server/
|
|
|
140
229
|
[template/server/docs/deployment.md](template/server/docs/deployment.md)), which also explains why a build without
|
|
141
230
|
Docker's cache takes about 6 to 7 minutes longer (it compiles libvips).
|
|
142
231
|
|
|
143
|
-
**The server needs a persistent volume for uploads.** Avatars
|
|
232
|
+
**The server needs a persistent volume for uploads.** Avatars and other uploaded files are written to `/app/uploads`
|
|
233
|
+
inside the container, and every deploy starts a fresh container, so without a volume they are lost on each redeploy.
|
|
234
|
+
In the server application: **Storages → Add Volume Mount**, leave *Source Path* empty (Coolify manages the volume),
|
|
235
|
+
*Destination Path* `/app/uploads`. The server warns at boot (`[STORAGE] … is not on a mounted volume`) when it runs in
|
|
236
|
+
production without one.
|
|
237
|
+
|
|
238
|
+
**First admin.** Sign up in the app with the account that should be the admin (verify its email if the email module
|
|
239
|
+
is on). Then open the server application's **Terminal** in Coolify and run:
|
|
240
|
+
|
|
241
|
+
```bash
|
|
242
|
+
node dist/scripts/make-admin.js you@example.com
|
|
243
|
+
```
|
|
144
244
|
|
|
145
|
-
|
|
245
|
+
It only changes that account's role and marks its email verified: no password is asked for or printed. Locally:
|
|
246
|
+
`npm run make-admin -- you@example.com` in `server/`. The same command recovers a project whose last admin was
|
|
247
|
+
demoted or banned by mistake.
|
|
146
248
|
|
|
147
|
-
**Background jobs** run inside the server process, so no extra Coolify service is needed
|
|
148
|
-
|
|
149
|
-
- deleted accounts past their 30 days: daily;
|
|
150
|
-
- Cloudflare's IP list: daily.
|
|
249
|
+
**Background jobs** run inside the server process, so no extra Coolify service is needed, and with more than one
|
|
250
|
+
server replica each runs them too, which is safe.
|
|
151
251
|
|
|
152
|
-
|
|
252
|
+
## Adding a module later
|
|
153
253
|
|
|
154
|
-
|
|
254
|
+
Modules add a feature to an existing project. Today there is one:
|
|
155
255
|
|
|
156
|
-
|
|
157
|
-
-
|
|
158
|
-
-
|
|
256
|
+
```bash
|
|
257
|
+
cd my-app # the folder that holds server/ and client/
|
|
258
|
+
npx create-tigra add email-verification
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
It adds the verification routes (`POST /auth/send-verification`, `POST /auth/verify-account`), their integration
|
|
262
|
+
tests, the client's `useVerifyAccount` / `useSendVerification` hooks and service, and sets `REQUIRE_USER_VERIFICATION=true` in `server/.env`.
|
|
263
|
+
Then set `RESEND_API_KEY` in `server/.env` and restart the server. Answering "yes" to the email question when you
|
|
264
|
+
create a project does the same. Run it on projects made with 4.x: it copies tests that use the 4.x test helpers.
|
|
265
|
+
|
|
266
|
+
## Documentation
|
|
267
|
+
|
|
268
|
+
In every generated project:
|
|
269
|
+
|
|
270
|
+
| File | What it explains |
|
|
271
|
+
|---|---|
|
|
272
|
+
| `README.md`, `server/README.md`, `client/README.md` | what is where, how to run it |
|
|
273
|
+
| `server/docs/deployment.md` | what a deploy does, safe migrations (expand, deploy, contract), rollback, build time |
|
|
274
|
+
| `server/docs/rate-limits.md` | every limit, the login lockout, IP blocks, email caps, env overrides, Redis outages |
|
|
275
|
+
| `server/docs/testing.md` | unit and integration tests, writing new ones, CI, coverage |
|
|
276
|
+
| `server/docs/logging.md` | request logs, redaction, health checks |
|
|
277
|
+
| `server/.env.example`, `server/.env.example.production`, `client/.env.example*` | every setting, with notes |
|
|
278
|
+
| `AGENTS.md`, `server/AGENTS.md`, `client/AGENTS.md` | the rules agents (and people) follow when changing the code |
|
|
279
|
+
|
|
280
|
+
In this repository:
|
|
281
|
+
|
|
282
|
+
- [CHANGELOG.md](CHANGELOG.md): what changed in each release.
|
|
283
|
+
- [Client IP behind proxies and Cloudflare](docs/client-ip-behind-proxies.md): how the visitor address is verified,
|
|
284
|
+
and how to port it to an older project.
|
|
285
|
+
- [Account ban vs email verification](docs/account-ban-vs-email-verification.md) and
|
|
286
|
+
[Account deactivation and login](docs/account-deactivation-login-reactivation.md): two account-state fixes, with
|
|
287
|
+
porting steps.
|
|
288
|
+
- [Rate limits](template/server/docs/rate-limits.md), [deployment](template/server/docs/deployment.md),
|
|
289
|
+
[testing](template/server/docs/testing.md) and [logging](template/server/docs/logging.md): the same guides a
|
|
290
|
+
generated project ships.
|
|
291
|
+
|
|
292
|
+
## Upgrading from 3.x
|
|
293
|
+
|
|
294
|
+
A generated project never updates itself: it is your code from the moment it is created. 4.0 changed most of the
|
|
295
|
+
template (sessions, rate limits, uploads, the admin API, jobs, health checks, tests, the Docker images, the agent
|
|
296
|
+
rules and the client). [CHANGELOG.md](CHANGELOG.md) lists every change by area; the guides in `docs/` show how to port
|
|
297
|
+
the ones that need care. New projects need Node 24 and npm 11.19+ (the 3.3.0 images ran Node 20).
|
|
159
298
|
|
|
160
299
|
## License
|
|
161
300
|
|
package/bin/create-tigra.js
CHANGED
|
@@ -24,6 +24,8 @@ const JWT_SECRET_PLACEHOLDER = 'CHANGE_ME_generate_with_openssl_rand_hex_48';
|
|
|
24
24
|
// Files that contain template variables and need replacement
|
|
25
25
|
const FILES_TO_REPLACE = [
|
|
26
26
|
'AGENTS.md',
|
|
27
|
+
// The project's own README: its name, database and local ports.
|
|
28
|
+
'README.md',
|
|
27
29
|
'server/package.json',
|
|
28
30
|
'server/.env.example',
|
|
29
31
|
'server/docker-compose.yml',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-tigra",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Create a production-ready full-stack app with Next.js 16 + Fastify 5 + Prisma + Redis",
|
|
6
6
|
"bin": {
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
"bin",
|
|
11
11
|
"lib",
|
|
12
12
|
"modules",
|
|
13
|
-
"template"
|
|
13
|
+
"template",
|
|
14
|
+
"CHANGELOG.md"
|
|
14
15
|
],
|
|
15
16
|
"engines": {
|
|
16
17
|
"node": ">=22.12.0"
|
|
@@ -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 |
|
|
@@ -1,36 +1,107 @@
|
|
|
1
|
-
|
|
1
|
+
# Client
|
|
2
2
|
|
|
3
|
-
|
|
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
|
-
|
|
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
|
-
#
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
##
|
|
89
|
+
## Security headers
|
|
24
90
|
|
|
25
|
-
|
|
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
|
-
|
|
28
|
-
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
|
|
95
|
+
## Tests
|
|
29
96
|
|
|
30
|
-
|
|
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
|
-
##
|
|
101
|
+
## Deploying
|
|
33
102
|
|
|
34
|
-
The
|
|
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
|
-
|
|
107
|
+
`AGENTS.md` in this folder has the rules for changing this code.
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# Server
|
|
2
|
+
|
|
3
|
+
The API: Fastify 5 and TypeScript, Prisma 6 on MySQL 8.4, Redis 7 (ioredis), Zod, Pino, Vitest. Every route lives
|
|
4
|
+
under `/api/v1`.
|
|
5
|
+
|
|
6
|
+
## Layout
|
|
7
|
+
|
|
8
|
+
```text
|
|
9
|
+
server/
|
|
10
|
+
├── src/
|
|
11
|
+
│ ├── app.ts builds the app: plugins, security hooks, routes
|
|
12
|
+
│ ├── server.ts starts it: database, Redis, jobs, graceful shutdown
|
|
13
|
+
│ ├── config/ env.ts (every setting, validated at boot), rate-limit.config.ts (every limit)
|
|
14
|
+
│ ├── modules/<domain>/ auth, users, admin, files: routes → controller → service → repo (+ schemas)
|
|
15
|
+
│ ├── libs/ shared pieces: auth, cookies, client IP, rate limiting, IP blocks, email,
|
|
16
|
+
│ │ HTTP clients, storage, logging, health, shutdown, validation errors
|
|
17
|
+
│ ├── jobs/ background jobs and their scheduler
|
|
18
|
+
│ ├── scripts/ make-admin
|
|
19
|
+
│ ├── shared/ response helpers, error classes, shared types
|
|
20
|
+
│ └── test/ integration test setup and helpers
|
|
21
|
+
├── prisma/ schema.prisma and migrations/ (starting with 0_init)
|
|
22
|
+
├── docs/ deployment, rate limits, testing, logging
|
|
23
|
+
├── postman/ a collection of every route
|
|
24
|
+
├── uploads/ local uploads (a volume at /app/uploads in production)
|
|
25
|
+
├── docker-compose.yml local MySQL and Redis (+ phpMyAdmin and Redis Commander with --profile tools)
|
|
26
|
+
└── Dockerfile the production image
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Commands
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm run dev # the API with reload, on PORT (8000)
|
|
33
|
+
npm test # unit tests, no database
|
|
34
|
+
npm run test:coverage # unit tests with the coverage floor (vitest.config.ts)
|
|
35
|
+
npm run test:integration # every route against a real MySQL and Redis (docs/testing.md)
|
|
36
|
+
npm run lint && npm run typecheck
|
|
37
|
+
npm run build && npm start # production build, then run it
|
|
38
|
+
|
|
39
|
+
npm run prisma:migrate:dev # after changing schema.prisma: create and apply a migration
|
|
40
|
+
npm run prisma:studio # browse the database
|
|
41
|
+
npm run make-admin -- you@example.com
|
|
42
|
+
npm run docker:up | docker:tools | docker:down
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Routes
|
|
46
|
+
|
|
47
|
+
| Route | Who may call it |
|
|
48
|
+
|---|---|
|
|
49
|
+
| `POST /auth/register`, `/auth/login`, `/auth/refresh`, `/auth/logout` | anyone |
|
|
50
|
+
| `POST /auth/forgot-password`, `/auth/reset-password` | anyone (the emailed link carries the token) |
|
|
51
|
+
| `GET /auth/me`, `GET /auth/sessions`, `POST /auth/logout-all` | signed in |
|
|
52
|
+
| `PATCH /users/me`, `PATCH /users/me/password`, `DELETE /users/me` | signed in |
|
|
53
|
+
| `POST /users/avatar`, `DELETE /users/avatar` | signed in |
|
|
54
|
+
| `PATCH /users/:userId`, `/users/:userId/password`, `DELETE /users/:userId`, `POST` and `DELETE /users/:userId/avatar` | the owner, or an admin |
|
|
55
|
+
| `GET /files/:filename` | the file's owner |
|
|
56
|
+
| `GET /admin/stats`, `/admin/health`, `/admin/client-ip` | admin |
|
|
57
|
+
| `GET /admin/users`, `GET /admin/users/:userId`, `PATCH /admin/users/:userId/status` and `/role`, `POST /admin/users/:userId/restore` | admin |
|
|
58
|
+
| `GET /admin/sessions`, `DELETE /admin/sessions/:sessionId` | admin |
|
|
59
|
+
| `GET /admin/blocked-ips`, `POST /admin/blocked-ips`, `DELETE /admin/blocked-ips/:ip` | admin |
|
|
60
|
+
| `GET /health`, `/ready`, `/live` | anyone (never rate limited) |
|
|
61
|
+
|
|
62
|
+
With the email-verification module: `POST /auth/send-verification` and `POST /auth/verify-account`. Public avatars are
|
|
63
|
+
served from `/uploads/`. `postman/collection.json` has every route with example bodies.
|
|
64
|
+
|
|
65
|
+
Every answer has one shape:
|
|
66
|
+
|
|
67
|
+
```json
|
|
68
|
+
{ "success": true, "message": "Profile updated", "data": { } }
|
|
69
|
+
{ "success": false, "error": { "code": "VALIDATION_FAILED", "message": "Please check these fields: email", "fields": [{ "field": "email", "message": "Invalid email address" }] } }
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Wrong input is always `422 VALIDATION_FAILED` with `fields`; "slow down" answers (429, and 403 `IP_BLOCKED`) carry
|
|
73
|
+
`retryAfterSeconds`; an unexpected error is a generic 500, logged in full here.
|
|
74
|
+
|
|
75
|
+
## Adding a feature
|
|
76
|
+
|
|
77
|
+
1. A module in `src/modules/<domain>/`: `<domain>.routes.ts` (paths and Zod schemas), `.controller.ts` (HTTP in and
|
|
78
|
+
out), `.service.ts` (business rules and who may do what), `.repo.ts` (Prisma queries only), `.schemas.ts`.
|
|
79
|
+
2. Register its routes in `src/app.ts` under the `/api/v1` prefix. A new route gets the default limit of 200 per
|
|
80
|
+
minute; give it a `RATE_LIMITS` entry in `src/config/rate-limit.config.ts` only when it needs another number.
|
|
81
|
+
3. A schema change: edit `prisma/schema.prisma`, then `npm run prisma:migrate:dev`. Migrations run while the old
|
|
82
|
+
version still serves, so never drop, rename, retype or tighten a column in the same deploy as the code that
|
|
83
|
+
stops using it (`docs/deployment.md`).
|
|
84
|
+
4. Tests: unit tests next to the code (`__tests__/`), and the route's contract (401/403, 422, success) in
|
|
85
|
+
`src/__tests__/routes.integration.test.ts`. That file fails when a registered route is never called.
|
|
86
|
+
5. Fetching a URL a user gave you: `safeHttpClient` (`src/libs/safe-http.ts`), never `fetch`. Recurring work:
|
|
87
|
+
`startRecurringJob` (`src/jobs/schedule.ts`), never `setInterval`.
|
|
88
|
+
|
|
89
|
+
`AGENTS.md` in this folder has the full set of rules.
|
|
90
|
+
|
|
91
|
+
## Configuration
|
|
92
|
+
|
|
93
|
+
`src/config/env.ts` validates every setting at boot and stops with the reason when one is wrong. `.env.example`
|
|
94
|
+
documents development, `.env.example.production` documents production. In production the server also refuses to
|
|
95
|
+
start:
|
|
96
|
+
|
|
97
|
+
- with a placeholder `JWT_SECRET`, or a `CORS_ORIGIN` that is not a list of bare origins;
|
|
98
|
+
- without `REDIS_URL`, or when Redis is not reachable within 10 s (the limits, the lockout and the IP blocks would
|
|
99
|
+
be off);
|
|
100
|
+
- without any migration in the image;
|
|
101
|
+
- with the placeholder Resend key or sender, when email is set up.
|
|
102
|
+
|
|
103
|
+
## Guides
|
|
104
|
+
|
|
105
|
+
- `docs/deployment.md`: what a deploy does, safe migrations, rollback, build time.
|
|
106
|
+
- `docs/rate-limits.md`: every limit, the login lockout, IP blocks, email caps, overrides, Redis outages.
|
|
107
|
+
- `docs/testing.md`: the unit and integration suites, writing tests, CI, coverage.
|
|
108
|
+
- `docs/logging.md`: request logs, redaction, health checks.
|
|
@@ -85,13 +85,27 @@ Redis is down or slow (so rate limits and IP blocks are off), or the heap is abo
|
|
|
85
85
|
|
|
86
86
|
The public answer never says which check failed. Admins get the full report at
|
|
87
87
|
`GET /api/v1/admin/health`: per check status and timing, memory (heap used and limit, RSS),
|
|
88
|
-
uptime, `checkedAt`, `version` (from `package.json`) and `commit` (
|
|
89
|
-
|
|
88
|
+
uptime, `checkedAt`, `version` (from `package.json`) and `commit` (the first 12 characters of
|
|
89
|
+
`SOURCE_COMMIT` when the environment provides it, otherwise `null`).
|
|
90
90
|
|
|
91
91
|
The database and Redis are checked at most once per 5 s; every probe in between gets the same
|
|
92
92
|
answer (`checkedAt` says when it was taken). A failing check logs the error at `error`
|
|
93
93
|
(database) or `warn` (Redis).
|
|
94
94
|
|
|
95
|
+
## Secrets never reach the logs
|
|
96
|
+
|
|
97
|
+
Pino's redaction (`src/libs/logger.ts`) removes these keys wherever they appear in a logged object, up to three
|
|
98
|
+
levels deep, and the secret headers of a logged request:
|
|
99
|
+
|
|
100
|
+
```text
|
|
101
|
+
keys password newPassword currentPassword passwordHash token accessToken refreshToken
|
|
102
|
+
access_token refresh_token secret clientSecret apiKey api_key authorization cookie
|
|
103
|
+
headers authorization cookie set-cookie proxy-authorization x-api-key x-origin-verify
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Request lines never contain the query string, outbound request logs show URLs without keys or `user:password@`,
|
|
107
|
+
and email addresses in email logs are masked. Logging a secret under another name is still on you: name it plainly.
|
|
108
|
+
|
|
95
109
|
## Configuration
|
|
96
110
|
|
|
97
111
|
### Development
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Testing
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The server has two suites, both Vitest (the client's unit tests are at the end):
|
|
4
4
|
|
|
5
5
|
| Suite | Command | Needs | What it proves |
|
|
6
6
|
|---|---|---|---|
|
|
@@ -42,6 +42,15 @@ applies, so a migration that does not match the code fails here first.
|
|
|
42
42
|
`uploadAvatar()`, and `startEmailStub()`, a local stand-in for Resend that catches every email (nothing is sent).
|
|
43
43
|
- Every new route gets a row in `src/__tests__/routes.integration.test.ts`: who may call it (401/403), what wrong
|
|
44
44
|
input answers (422) and its success answer. Deeper behaviour goes in the area's own integration file.
|
|
45
|
+
- That file checks itself: its last test fails, naming the route, when a registered route is never called in it.
|
|
46
|
+
A route another file already tests goes in its `COVERED_ELSEWHERE` list with that file's name:
|
|
47
|
+
|
|
48
|
+
```text
|
|
49
|
+
× every registered route was called above, or is in COVERED_ELSEWHERE
|
|
50
|
+
+ "GET /api/v1/reports/:id"
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Run it on its own with `-t` and it skips itself (the other tests did not run).
|
|
45
54
|
- Import the app lazily, inside `beforeAll` (`const { buildApp } = await import('@/app.js')`): the environment is
|
|
46
55
|
set by `src/test/integration.setup.ts` and must be in place before `env.ts` is loaded.
|
|
47
56
|
- All files share one database and Redis and run one after another. Create your own data with unique emails; never
|
|
@@ -53,7 +62,7 @@ applies, so a migration that does not match the code fails here first.
|
|
|
53
62
|
|
|
54
63
|
`.github/workflows/ci.yml` (project root) runs on every push and pull request: typecheck, lint,
|
|
55
64
|
`npm run test:coverage`, the server build, `npm run test:integration` (Testcontainers on GitHub's Ubuntu runners)
|
|
56
|
-
and the client's typecheck, lint and build. It never deploys.
|
|
65
|
+
and the client's typecheck, lint, `npm run test:coverage` and build. It never deploys.
|
|
57
66
|
|
|
58
67
|
## Coverage
|
|
59
68
|
|
|
@@ -61,3 +70,12 @@ and the client's typecheck, lint and build. It never deploys.
|
|
|
61
70
|
thresholds in `vitest.config.ts`. They are a ratchet: the real numbers when they were set, rounded down. Raise them
|
|
62
71
|
when you add tests; never lower them to make a change pass. Routes and controllers are tested by the integration
|
|
63
72
|
suite, which the coverage gate does not count.
|
|
73
|
+
|
|
74
|
+
## Client tests
|
|
75
|
+
|
|
76
|
+
`client/` has its own Vitest unit suite (`npm test`, `npm run test:coverage` with its floor in
|
|
77
|
+
`client/vitest.config.mts`). It covers the logic under the screens: the API client's session rules (one refresh for
|
|
78
|
+
parallel 401s, a refresh race lost to another tab, a ban mid-session), the proxy's CSP and redirects, redirect
|
|
79
|
+
safety, path matching, the session marker, error and field-error parsing, and the store. The template ships no pages;
|
|
80
|
+
when you add them, keep their logic in functions under `client/src/lib/` and test it there.
|
|
81
|
+
|