authkit-api 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -1
- package/README.md +21 -5
- package/package.json +12 -5
- package/src/constants.js +5 -0
- package/src/generator.js +37 -7
- package/src/index.js +12 -2
- package/src/prompts.js +20 -4
- package/templates/base/README.md +5 -5
- package/templates/base/package.json +4 -4
- package/templates/base/scripts/docker-entrypoint.sh +1 -1
- package/templates/base/scripts/generate-postman.ts +102 -0
- package/templates/base/scripts/seed-admin.ts +62 -0
- package/templates/base/src/app.ts +46 -0
- package/templates/base/src/config/env.schema.ts +85 -0
- package/templates/base/src/config/env.ts +15 -0
- package/templates/base/src/config/logger.ts +20 -0
- package/templates/base/src/constants/examples.ts +3 -0
- package/templates/base/src/constants/roles.ts +4 -0
- package/templates/base/src/constants/user.rules.ts +16 -0
- package/templates/base/src/docs/openapi.registry.ts +90 -0
- package/templates/base/src/docs/openapi.ts +36 -0
- package/templates/base/src/docs/paths/auth.paths.ts +88 -0
- package/templates/base/src/docs/paths/user.paths.ts +88 -0
- package/templates/base/src/middlewares/authenticate.ts +20 -0
- package/templates/base/src/middlewares/authorize.ts +13 -0
- package/templates/base/src/middlewares/errorHandler.ts +52 -0
- package/templates/base/src/middlewares/notFound.ts +6 -0
- package/templates/base/src/middlewares/rateLimiter.ts +20 -0
- package/templates/base/src/middlewares/validate.ts +39 -0
- package/templates/base/src/modules/auth/auth.controller.ts +45 -0
- package/templates/base/src/modules/auth/auth.cookie.ts +22 -0
- package/templates/base/src/modules/auth/auth.routes.ts +12 -0
- package/templates/base/src/modules/auth/auth.service.ts +81 -0
- package/templates/base/src/modules/auth/auth.validation.ts +44 -0
- package/templates/base/src/modules/user/user.controller.ts +24 -0
- package/templates/base/src/modules/user/user.routes.ts +22 -0
- package/templates/base/src/modules/user/user.service.ts +57 -0
- package/templates/base/src/modules/user/user.validation.ts +27 -0
- package/templates/base/src/routes/docs.routes.ts +16 -0
- package/templates/base/src/routes/health.routes.ts +17 -0
- package/templates/base/src/routes/index.ts +13 -0
- package/templates/base/src/server.ts +51 -0
- package/templates/base/src/types/express.d.ts +14 -0
- package/templates/base/src/utils/ApiError.ts +35 -0
- package/templates/base/src/utils/apiResponse.ts +24 -0
- package/templates/base/src/utils/password.ts +14 -0
- package/templates/base/src/utils/token.ts +60 -0
- package/templates/base/src/utils/zod.ts +5 -0
- package/templates/base/tests/app.test.ts +67 -0
- package/templates/base/tests/auth.test.ts +194 -0
- package/templates/base/tests/authenticate.test.ts +61 -0
- package/templates/base/tests/docs.test.ts +127 -0
- package/templates/base/tests/env.test.ts +96 -0
- package/templates/base/tests/health.test.ts +21 -0
- package/templates/base/tests/helpers/auth.helper.ts +49 -0
- package/templates/base/tests/password.test.ts +18 -0
- package/templates/base/tests/postman.test.ts +32 -0
- package/templates/base/tests/rateLimiter.test.ts +27 -0
- package/templates/base/tests/seedAdmin.test.ts +42 -0
- package/templates/base/tests/token.test.ts +69 -0
- package/templates/base/tests/trustProxy.test.ts +24 -0
- package/templates/base/tests/user.repository.test.ts +211 -0
- package/templates/base/tests/user.test.ts +145 -0
- package/templates/base/tests/validate.test.ts +42 -0
- package/templates/base/tests/validation.test.ts +62 -0
- package/templates/base/vitest.config.ts +16 -0
- package/templates/databases/mongodb/README.part.md +1 -1
- package/templates/databases/mongodb/src/database/connection.ts +46 -0
- package/templates/databases/mongodb/src/database/env.schema.ts +3 -0
- package/templates/databases/mongodb/src/database/id.schema.ts +3 -0
- package/templates/databases/mongodb/src/database/models/refreshToken.model.ts +24 -0
- package/templates/databases/mongodb/src/database/models/user.model.ts +52 -0
- package/templates/databases/mongodb/src/modules/user/user.repository.ts +91 -0
- package/templates/databases/mongodb/tests/globalSetup.ts +13 -0
- package/templates/databases/mongodb/tests/setup.ts +17 -0
- package/templates/databases/postgres/README.part.md +1 -1
- package/templates/databases/postgres/prisma.config.ts +14 -0
- package/templates/databases/postgres/src/database/connection.ts +72 -0
- package/templates/databases/postgres/src/database/env.schema.ts +3 -0
- package/templates/databases/postgres/src/database/id.schema.ts +3 -0
- package/templates/databases/postgres/src/database/prisma.ts +9 -0
- package/templates/databases/postgres/src/modules/user/user.repository.ts +111 -0
- package/templates/databases/postgres/tests/connection.test.ts +19 -0
- package/templates/databases/postgres/tests/globalSetup.ts +66 -0
- package/templates/databases/postgres/tests/prismaSchema.test.ts +27 -0
- package/templates/databases/postgres/tests/setup.ts +15 -0
- package/templates/languages/ts/eslint.config.js +30 -0
- package/templates/languages/ts/package.part.json +21 -0
- package/templates/languages/ts/tsconfig.json +15 -0
- package/templates-js/base/Dockerfile +22 -0
- package/templates-js/base/README.md +108 -0
- package/templates-js/base/_dockerignore +16 -0
- package/templates-js/base/_env.example +25 -0
- package/templates-js/base/_gitignore +5 -0
- package/templates-js/base/_husky/pre-commit +1 -0
- package/templates-js/base/_prettierignore +5 -0
- package/templates-js/base/_prettierrc +1 -0
- package/templates-js/base/package.json +62 -0
- package/templates-js/base/scripts/docker-entrypoint.sh +9 -0
- package/templates-js/databases/mongodb/README.part.md +23 -0
- package/templates-js/databases/mongodb/_github/workflows/ci.yml +29 -0
- package/templates-js/databases/mongodb/docker-compose.yml +30 -0
- package/templates-js/databases/mongodb/env.part +2 -0
- package/templates-js/databases/mongodb/package.part.json +4 -0
- package/templates-js/databases/postgres/README.part.md +31 -0
- package/templates-js/databases/postgres/_github/workflows/ci.yml +46 -0
- package/templates-js/databases/postgres/docker-compose.yml +34 -0
- package/templates-js/databases/postgres/env.part +2 -0
- package/templates-js/databases/postgres/package.part.json +15 -0
- package/templates-js/databases/postgres/prisma/migrations/20261030073256_init/migration.sql +39 -0
- package/templates-js/databases/postgres/prisma/migrations/migration_lock.toml +3 -0
- package/templates-js/databases/postgres/prisma/schema.prisma +39 -0
- /package/templates/{base → languages/js}/eslint.config.js +0 -0
- /package/{templates → templates-js}/base/scripts/generate-postman.js +0 -0
- /package/{templates → templates-js}/base/scripts/seed-admin.js +0 -0
- /package/{templates → templates-js}/base/src/app.js +0 -0
- /package/{templates → templates-js}/base/src/config/env.js +0 -0
- /package/{templates → templates-js}/base/src/config/env.schema.js +0 -0
- /package/{templates → templates-js}/base/src/config/logger.js +0 -0
- /package/{templates → templates-js}/base/src/constants/examples.js +0 -0
- /package/{templates → templates-js}/base/src/constants/roles.js +0 -0
- /package/{templates → templates-js}/base/src/constants/user.rules.js +0 -0
- /package/{templates → templates-js}/base/src/docs/openapi.js +0 -0
- /package/{templates → templates-js}/base/src/docs/openapi.registry.js +0 -0
- /package/{templates → templates-js}/base/src/docs/paths/auth.paths.js +0 -0
- /package/{templates → templates-js}/base/src/docs/paths/user.paths.js +0 -0
- /package/{templates → templates-js}/base/src/middlewares/authenticate.js +0 -0
- /package/{templates → templates-js}/base/src/middlewares/authorize.js +0 -0
- /package/{templates → templates-js}/base/src/middlewares/errorHandler.js +0 -0
- /package/{templates → templates-js}/base/src/middlewares/notFound.js +0 -0
- /package/{templates → templates-js}/base/src/middlewares/rateLimiter.js +0 -0
- /package/{templates → templates-js}/base/src/middlewares/validate.js +0 -0
- /package/{templates → templates-js}/base/src/modules/auth/auth.controller.js +0 -0
- /package/{templates → templates-js}/base/src/modules/auth/auth.cookie.js +0 -0
- /package/{templates → templates-js}/base/src/modules/auth/auth.routes.js +0 -0
- /package/{templates → templates-js}/base/src/modules/auth/auth.service.js +0 -0
- /package/{templates → templates-js}/base/src/modules/auth/auth.validation.js +0 -0
- /package/{templates → templates-js}/base/src/modules/user/user.controller.js +0 -0
- /package/{templates → templates-js}/base/src/modules/user/user.routes.js +0 -0
- /package/{templates → templates-js}/base/src/modules/user/user.service.js +0 -0
- /package/{templates → templates-js}/base/src/modules/user/user.validation.js +0 -0
- /package/{templates → templates-js}/base/src/routes/docs.routes.js +0 -0
- /package/{templates → templates-js}/base/src/routes/health.routes.js +0 -0
- /package/{templates → templates-js}/base/src/routes/index.js +0 -0
- /package/{templates → templates-js}/base/src/server.js +0 -0
- /package/{templates → templates-js}/base/src/utils/ApiError.js +0 -0
- /package/{templates → templates-js}/base/src/utils/apiResponse.js +0 -0
- /package/{templates → templates-js}/base/src/utils/password.js +0 -0
- /package/{templates → templates-js}/base/src/utils/token.js +0 -0
- /package/{templates → templates-js}/base/src/utils/zod.js +0 -0
- /package/{templates → templates-js}/base/tests/app.test.js +0 -0
- /package/{templates → templates-js}/base/tests/auth.test.js +0 -0
- /package/{templates → templates-js}/base/tests/authenticate.test.js +0 -0
- /package/{templates → templates-js}/base/tests/docs.test.js +0 -0
- /package/{templates → templates-js}/base/tests/env.test.js +0 -0
- /package/{templates → templates-js}/base/tests/health.test.js +0 -0
- /package/{templates → templates-js}/base/tests/helpers/auth.helper.js +0 -0
- /package/{templates → templates-js}/base/tests/password.test.js +0 -0
- /package/{templates → templates-js}/base/tests/postman.test.js +0 -0
- /package/{templates → templates-js}/base/tests/rateLimiter.test.js +0 -0
- /package/{templates → templates-js}/base/tests/seedAdmin.test.js +0 -0
- /package/{templates → templates-js}/base/tests/token.test.js +0 -0
- /package/{templates → templates-js}/base/tests/trustProxy.test.js +0 -0
- /package/{templates → templates-js}/base/tests/user.repository.test.js +0 -0
- /package/{templates → templates-js}/base/tests/user.test.js +0 -0
- /package/{templates → templates-js}/base/tests/validate.test.js +0 -0
- /package/{templates → templates-js}/base/tests/validation.test.js +0 -0
- /package/{templates → templates-js}/base/vitest.config.js +0 -0
- /package/{templates → templates-js}/databases/mongodb/src/database/connection.js +0 -0
- /package/{templates → templates-js}/databases/mongodb/src/database/env.schema.js +0 -0
- /package/{templates → templates-js}/databases/mongodb/src/database/id.schema.js +0 -0
- /package/{templates → templates-js}/databases/mongodb/src/database/models/refreshToken.model.js +0 -0
- /package/{templates → templates-js}/databases/mongodb/src/database/models/user.model.js +0 -0
- /package/{templates → templates-js}/databases/mongodb/src/modules/user/user.repository.js +0 -0
- /package/{templates → templates-js}/databases/mongodb/tests/globalSetup.js +0 -0
- /package/{templates → templates-js}/databases/mongodb/tests/setup.js +0 -0
- /package/{templates → templates-js}/databases/postgres/prisma.config.js +0 -0
- /package/{templates → templates-js}/databases/postgres/src/database/connection.js +0 -0
- /package/{templates → templates-js}/databases/postgres/src/database/env.schema.js +0 -0
- /package/{templates → templates-js}/databases/postgres/src/database/id.schema.js +0 -0
- /package/{templates → templates-js}/databases/postgres/src/database/prisma.js +0 -0
- /package/{templates → templates-js}/databases/postgres/src/modules/user/user.repository.js +0 -0
- /package/{templates → templates-js}/databases/postgres/tests/connection.test.js +0 -0
- /package/{templates → templates-js}/databases/postgres/tests/globalSetup.js +0 -0
- /package/{templates → templates-js}/databases/postgres/tests/prismaSchema.test.js +0 -0
- /package/{templates → templates-js}/databases/postgres/tests/setup.js +0 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# {{projectName}}
|
|
2
|
+
|
|
3
|
+
Express authentication API generated by authkit-api.
|
|
4
|
+
|
|
5
|
+
## What you get
|
|
6
|
+
|
|
7
|
+
- Register, login, refresh and logout with JWT access tokens and rotating refresh tokens (httpOnly cookie)
|
|
8
|
+
- Roles (`user`, `admin`), a `/users/me` endpoint and admin-only user management
|
|
9
|
+
- Validation (Zod), rate limiting, Helmet, CORS, structured logs (pino)
|
|
10
|
+
- Swagger UI, a Postman export, tests (Vitest), ESLint and Prettier
|
|
11
|
+
- A Dockerfile, a compose file and a GitHub Actions workflow
|
|
12
|
+
|
|
13
|
+
{{databaseSection}}
|
|
14
|
+
|
|
15
|
+
## Environment variables
|
|
16
|
+
|
|
17
|
+
Copy `.env.example` to `.env`. The database variable is listed in the Database section above.
|
|
18
|
+
|
|
19
|
+
| Variable | Meaning |
|
|
20
|
+
| ------------------------------------------------- | --------------------------------------------------------------- |
|
|
21
|
+
| `NODE_ENV` | `development`, `test` or `production` |
|
|
22
|
+
| `PORT` | Port the API listens on (default `3000`) |
|
|
23
|
+
| `LOG_LEVEL` | pino log level (default `info`) |
|
|
24
|
+
| `CORS_ORIGIN` | Allowed browser origin |
|
|
25
|
+
| `TRUST_PROXY` | Express `trust proxy`. Set `1` behind one reverse proxy or PaaS |
|
|
26
|
+
| `JWT_ACCESS_SECRET`, `JWT_REFRESH_SECRET` | Two different values of 32+ characters: `openssl rand -hex 32` |
|
|
27
|
+
| `JWT_ACCESS_EXPIRES_IN`, `JWT_REFRESH_EXPIRES_IN` | Token lifetimes, for example `15m` and `7d` |
|
|
28
|
+
| `AUTH_RATE_LIMIT_MAX` | Login/register requests allowed per window |
|
|
29
|
+
| `ADMIN_NAME`, `ADMIN_EMAIL`, `ADMIN_PASSWORD` | Used by `npm run seed:admin` |
|
|
30
|
+
| `API_DOCS_ENABLED` | `true` or `false`. Swagger is off in production by default |
|
|
31
|
+
|
|
32
|
+
## Scripts
|
|
33
|
+
|
|
34
|
+
| Script | Does |
|
|
35
|
+
| ----------------------------------- | ------------------------------------------------------ |
|
|
36
|
+
| `npm run dev` | Start with auto-reload, reading `.env` |
|
|
37
|
+
| `npm start` | Start for production (env from the environment) |
|
|
38
|
+
| `npm test` | Run the tests once (`test:watch`, `test:coverage` too) |
|
|
39
|
+
| `npm run lint` | ESLint (`lint:fix` repairs) |
|
|
40
|
+
| `npm run format` / `format:check` | Prettier write / check |
|
|
41
|
+
| `npm run seed:admin` | Create the first admin |
|
|
42
|
+
| `npm run docs:postman` | Write `postman/collection.json` |
|
|
43
|
+
| `npm run docker:up` / `docker:down` | Start / stop the app and database in Docker |
|
|
44
|
+
|
|
45
|
+
## API
|
|
46
|
+
|
|
47
|
+
All routes are under `/api/v1`. Successful responses look like `{ "success": true, "message": "...", "data": { ... } }`.
|
|
48
|
+
|
|
49
|
+
| Method | Path | Who | Does |
|
|
50
|
+
| ------ | ---------------- | ------ | ------------------------------------------------- |
|
|
51
|
+
| POST | `/auth/register` | all | Create a user |
|
|
52
|
+
| POST | `/auth/login` | all | Get an access token and refresh cookie |
|
|
53
|
+
| POST | `/auth/refresh` | cookie | Rotate the refresh token, new access token |
|
|
54
|
+
| POST | `/auth/logout` | cookie | Revoke the refresh token |
|
|
55
|
+
| GET | `/users/me` | user | Current user |
|
|
56
|
+
| GET | `/users` | admin | List users (`?page=1&limit=20`) |
|
|
57
|
+
| GET | `/users/:id` | admin | One user |
|
|
58
|
+
| DELETE | `/users/:id` | admin | Delete a user |
|
|
59
|
+
| GET | `/health` | all | Liveness and database check (not under `/api/v1`) |
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
curl -X POST http://localhost:3000/api/v1/auth/register \
|
|
63
|
+
-H 'Content-Type: application/json' \
|
|
64
|
+
-d '{"name":"Ada","email":"ada@example.com","password":"Passw0rd123"}'
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## API docs
|
|
68
|
+
|
|
69
|
+
Open <http://localhost:3000/api-docs> (raw JSON at `/api-docs.json`). Click Authorize and paste the access token from login.
|
|
70
|
+
|
|
71
|
+
To use Postman: run `npm run docs:postman`, then in Postman choose Import and pick `postman/collection.json`. The collection saves the access token after you log in.
|
|
72
|
+
|
|
73
|
+
## First admin
|
|
74
|
+
|
|
75
|
+
Set `ADMIN_NAME`, `ADMIN_EMAIL` and `ADMIN_PASSWORD` in `.env`, then run `npm run seed:admin`. The example password from `.env.example` is refused. If that email already exists the user is promoted to admin; an existing password is never changed.
|
|
76
|
+
|
|
77
|
+
## Add a field to the user
|
|
78
|
+
|
|
79
|
+
Example: a `phone` number. Touch these files:
|
|
80
|
+
|
|
81
|
+
1. `src/constants/user.rules.js`: add the limits (for example `PHONE: { MAX: 20 }`).
|
|
82
|
+
2. `src/modules/auth/auth.validation.js`: add the field to `registerSchema`.
|
|
83
|
+
3. `src/modules/auth/auth.service.js` and the repository `create`: pass the field through.
|
|
84
|
+
4. The database files: see "Adding a user field" in the Database section above.
|
|
85
|
+
5. `src/docs/openapi.registry.js`: add the field to `PublicUser` so the docs stay correct.
|
|
86
|
+
6. Add a test in `tests/user.test.js`.
|
|
87
|
+
|
|
88
|
+
## Docker
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
cp .env.example .env
|
|
92
|
+
# Docker runs in production mode, which refuses the example secrets.
|
|
93
|
+
# In .env, replace the existing JWT_ACCESS_SECRET= and JWT_REFRESH_SECRET= values
|
|
94
|
+
# with two different outputs of:
|
|
95
|
+
openssl rand -base64 48
|
|
96
|
+
npm run docker:up # build and start the app and the database
|
|
97
|
+
npm run docker:down # stop them
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
The app is then on <http://localhost:3000>. The container runs as the non-root `node` user, starts in production mode (so `/api-docs` is off) and has a health check on `/health`. If port 3000 is taken, run `PORT=3001 npm run docker:up`: this changes only the host port, the app listens on 3000 inside the container. For the database port, see the Database section above.
|
|
101
|
+
|
|
102
|
+
The GitHub Actions workflow in `.github/workflows/ci.yml` lints, checks formatting, tests and builds the image on every push. It needs `package-lock.json` committed. It does not push the image anywhere; to publish one, add a `docker/login-action` step and set `push: true` in `docker/build-push-action`.
|
|
103
|
+
|
|
104
|
+
## Notes
|
|
105
|
+
|
|
106
|
+
- Logout revokes the refresh token, but an access token already issued stays valid until it expires (`JWT_ACCESS_EXPIRES_IN`, 15 minutes by default). That is the trade-off of stateless access tokens. Keep them short.
|
|
107
|
+
- Serialize refresh calls in your frontend; parallel refreshes with the same cookie are treated as token reuse and log the user out.
|
|
108
|
+
- Node.js 22.18 or newer is required.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# {{projectName}} environment. Copy to .env and adjust.
|
|
2
|
+
NODE_ENV=development
|
|
3
|
+
PORT=3000
|
|
4
|
+
LOG_LEVEL=info
|
|
5
|
+
CORS_ORIGIN=http://localhost:5173
|
|
6
|
+
# Behind one reverse proxy or a PaaS (Render, Fly, Heroku...) set 1, so rate limits see the client IP.
|
|
7
|
+
# Also accepts true/false, a hop count, or names/CIDRs such as loopback,10.0.0.0/8. Unset: off.
|
|
8
|
+
# TRUST_PROXY=1
|
|
9
|
+
|
|
10
|
+
# JWT secrets must be at least 32 characters. Generate with: openssl rand -hex 32
|
|
11
|
+
JWT_ACCESS_SECRET=change-me-access-secret-at-least-32-chars
|
|
12
|
+
JWT_REFRESH_SECRET=change-me-refresh-secret-at-least-32-chars
|
|
13
|
+
JWT_ACCESS_EXPIRES_IN=15m
|
|
14
|
+
JWT_REFRESH_EXPIRES_IN=7d
|
|
15
|
+
|
|
16
|
+
# Rate limit for auth endpoints (requests per window)
|
|
17
|
+
AUTH_RATE_LIMIT_MAX=10
|
|
18
|
+
|
|
19
|
+
# Optional admin seed
|
|
20
|
+
ADMIN_NAME=Admin
|
|
21
|
+
ADMIN_EMAIL=admin@example.com
|
|
22
|
+
ADMIN_PASSWORD=ChangeMe123!
|
|
23
|
+
|
|
24
|
+
# Swagger UI at /api-docs; disabled in production by default
|
|
25
|
+
# API_DOCS_ENABLED=true
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
npx lint-staged
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "singleQuote": true, "semi": true, "trailingComma": "all", "printWidth": 100 }
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "authkit-api-template",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=22.18"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"dev": "node --watch --env-file=.env src/server.js",
|
|
11
|
+
"start": "node src/server.js",
|
|
12
|
+
"seed:admin": "node --env-file=.env scripts/seed-admin.js",
|
|
13
|
+
"test": "vitest run",
|
|
14
|
+
"test:watch": "vitest",
|
|
15
|
+
"test:coverage": "vitest run --coverage",
|
|
16
|
+
"lint": "eslint .",
|
|
17
|
+
"lint:fix": "eslint . --fix",
|
|
18
|
+
"format": "prettier --write .",
|
|
19
|
+
"format:check": "prettier --check .",
|
|
20
|
+
"prepare": "husky || true",
|
|
21
|
+
"docs:postman": "node --env-file-if-exists=.env scripts/generate-postman.js",
|
|
22
|
+
"docker:up": "docker compose up -d --build",
|
|
23
|
+
"docker:down": "docker compose down"
|
|
24
|
+
},
|
|
25
|
+
"lint-staged": {
|
|
26
|
+
"*.js": [
|
|
27
|
+
"eslint --fix",
|
|
28
|
+
"prettier --write"
|
|
29
|
+
],
|
|
30
|
+
"*.{json,md,yml}": [
|
|
31
|
+
"prettier --write"
|
|
32
|
+
]
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@asteasolutions/zod-to-openapi": "^9.1.0",
|
|
36
|
+
"argon2": "^0.45.1",
|
|
37
|
+
"cookie-parser": "^1.4.7",
|
|
38
|
+
"cors": "^2.8.6",
|
|
39
|
+
"express": "^5.3.0",
|
|
40
|
+
"express-rate-limit": "^8.7.2",
|
|
41
|
+
"helmet": "^8.3.0",
|
|
42
|
+
"jsonwebtoken": "^9.0.3",
|
|
43
|
+
"pino": "^10.4.0",
|
|
44
|
+
"pino-http": "^11.0.0",
|
|
45
|
+
"swagger-ui-express": "^5.0.1",
|
|
46
|
+
"zod": "^4.6.7"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@eslint/js": "^10.0.1",
|
|
50
|
+
"@vitest/coverage-v8": "^5.0.5",
|
|
51
|
+
"eslint": "^10.13.0",
|
|
52
|
+
"eslint-config-prettier": "^10.1.8",
|
|
53
|
+
"globals": "^17.13.0",
|
|
54
|
+
"husky": "^9.1.7",
|
|
55
|
+
"lint-staged": "^17.6.0",
|
|
56
|
+
"openapi-to-postmanv2": "^6.4.2",
|
|
57
|
+
"pino-pretty": "^13.2.0",
|
|
58
|
+
"prettier": "^3.9.10",
|
|
59
|
+
"supertest": "^7.3.1",
|
|
60
|
+
"vitest": "^5.0.5"
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
## Quick start
|
|
2
|
+
|
|
3
|
+
```bash
|
|
4
|
+
cd {{projectName}}
|
|
5
|
+
cp .env.example .env
|
|
6
|
+
npm install
|
|
7
|
+
docker compose up -d --wait db
|
|
8
|
+
npm run dev
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The API is on <http://localhost:3000>. `docker compose up -d --wait db` starts a MongoDB at the default `MONGODB_URI` (or point it at your own). `npm test` needs nothing installed: it uses an in-memory MongoDB.
|
|
12
|
+
|
|
13
|
+
## Database
|
|
14
|
+
|
|
15
|
+
MongoDB with Mongoose.
|
|
16
|
+
|
|
17
|
+
| Variable | Meaning |
|
|
18
|
+
| ------------- | --------------------------------- |
|
|
19
|
+
| `MONGODB_URI` | Connection string of your MongoDB |
|
|
20
|
+
|
|
21
|
+
**Docker port.** The compose database is published on host port `27017`. If you already run MongoDB there, pick another port: `MONGO_PORT=27018 npm run docker:up`.
|
|
22
|
+
|
|
23
|
+
**Adding a user field.** Add it to the schema in `src/database/models/user.model.js` and to `create` in `src/modules/user/user.repository.js`. Mongoose adds the field to responses automatically.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on: [push, pull_request]
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
test:
|
|
7
|
+
runs-on: ubuntu-latest
|
|
8
|
+
steps:
|
|
9
|
+
- uses: actions/checkout@v7
|
|
10
|
+
- uses: actions/setup-node@v7
|
|
11
|
+
with:
|
|
12
|
+
node-version: 24
|
|
13
|
+
cache: npm
|
|
14
|
+
- run: npm ci
|
|
15
|
+
- run: npm run lint
|
|
16
|
+
- run: npm run format:check
|
|
17
|
+
- run: npm test
|
|
18
|
+
|
|
19
|
+
docker:
|
|
20
|
+
needs: test
|
|
21
|
+
runs-on: ubuntu-latest
|
|
22
|
+
steps:
|
|
23
|
+
- uses: actions/checkout@v7
|
|
24
|
+
- uses: docker/setup-buildx-action@v4
|
|
25
|
+
- uses: docker/build-push-action@v7
|
|
26
|
+
with:
|
|
27
|
+
context: .
|
|
28
|
+
push: false
|
|
29
|
+
tags: app:ci
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
name: '{{projectName}}'
|
|
2
|
+
|
|
3
|
+
services:
|
|
4
|
+
app:
|
|
5
|
+
build: .
|
|
6
|
+
env_file: .env
|
|
7
|
+
environment:
|
|
8
|
+
NODE_ENV: production
|
|
9
|
+
PORT: 3000
|
|
10
|
+
MONGODB_URI: mongodb://db:27017/{{databaseName}}
|
|
11
|
+
depends_on:
|
|
12
|
+
db:
|
|
13
|
+
condition: service_healthy
|
|
14
|
+
ports:
|
|
15
|
+
- '${PORT:-3000}:3000'
|
|
16
|
+
|
|
17
|
+
db:
|
|
18
|
+
image: mongo:8
|
|
19
|
+
ports:
|
|
20
|
+
- '${MONGO_PORT:-27017}:27017'
|
|
21
|
+
volumes:
|
|
22
|
+
- mongodata:/data/db
|
|
23
|
+
healthcheck:
|
|
24
|
+
test: ['CMD', 'mongosh', '--quiet', '--eval', "db.adminCommand('ping').ok"]
|
|
25
|
+
interval: 5s
|
|
26
|
+
timeout: 5s
|
|
27
|
+
retries: 10
|
|
28
|
+
|
|
29
|
+
volumes:
|
|
30
|
+
mongodata:
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
## Quick start
|
|
2
|
+
|
|
3
|
+
```bash
|
|
4
|
+
cd {{projectName}}
|
|
5
|
+
cp .env.example .env
|
|
6
|
+
npm install
|
|
7
|
+
docker compose up -d --wait db
|
|
8
|
+
npm run db:deploy
|
|
9
|
+
npm run dev
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
The API is on <http://localhost:3000>. `npm test` needs the same running server: it takes `DATABASE_URL` from the environment, else from `.env` (else `postgresql://postgres:postgres@localhost:5432/app`), and creates and migrates a separate `<database>_test` database for itself.
|
|
13
|
+
|
|
14
|
+
## Database
|
|
15
|
+
|
|
16
|
+
PostgreSQL with Prisma. The generated client lives in `src/generated/prisma` and is rebuilt by `npm install` and `npm run db:generate`.
|
|
17
|
+
|
|
18
|
+
| Variable | Meaning |
|
|
19
|
+
| -------------- | ---------------------------------- |
|
|
20
|
+
| `DATABASE_URL` | Connection string of your Postgres |
|
|
21
|
+
|
|
22
|
+
| Script | Does |
|
|
23
|
+
| --------------------- | ------------------------------------------ |
|
|
24
|
+
| `npm run db:migrate` | Create and apply a migration (development) |
|
|
25
|
+
| `npm run db:deploy` | Apply existing migrations |
|
|
26
|
+
| `npm run db:generate` | Regenerate the Prisma client |
|
|
27
|
+
| `npm run db:studio` | Open Prisma Studio |
|
|
28
|
+
|
|
29
|
+
**Docker port.** The compose database is published on host port `5432`. If you already run Postgres there, pick another port, for example `POSTGRES_PORT=5433`: set it when you run `docker compose up -d --wait db` or `npm run docker:up`, and put the same port in `DATABASE_URL` in `.env` (tests read it too). In Docker, the container applies migrations itself on start.
|
|
30
|
+
|
|
31
|
+
**Adding a user field.** Add the column to `model User` in `prisma/schema.prisma`, run `npm run db:migrate -- --name add_phone`, then add the field to `publicUserSelect` and to `create` in `src/modules/user/user.repository.js`.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on: [push, pull_request]
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
test:
|
|
7
|
+
runs-on: ubuntu-latest
|
|
8
|
+
services:
|
|
9
|
+
postgres:
|
|
10
|
+
image: postgres:17-alpine
|
|
11
|
+
env:
|
|
12
|
+
POSTGRES_USER: postgres
|
|
13
|
+
POSTGRES_PASSWORD: postgres
|
|
14
|
+
POSTGRES_DB: app
|
|
15
|
+
ports:
|
|
16
|
+
- 5432:5432
|
|
17
|
+
options: >-
|
|
18
|
+
--health-cmd "pg_isready -U postgres"
|
|
19
|
+
--health-interval 5s
|
|
20
|
+
--health-timeout 5s
|
|
21
|
+
--health-retries 10
|
|
22
|
+
env:
|
|
23
|
+
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/app
|
|
24
|
+
steps:
|
|
25
|
+
- uses: actions/checkout@v7
|
|
26
|
+
- uses: actions/setup-node@v7
|
|
27
|
+
with:
|
|
28
|
+
node-version: 24
|
|
29
|
+
cache: npm
|
|
30
|
+
- run: npm ci
|
|
31
|
+
- run: npm run lint
|
|
32
|
+
- run: npm run format:check
|
|
33
|
+
- run: npm run db:deploy
|
|
34
|
+
- run: npm test
|
|
35
|
+
|
|
36
|
+
docker:
|
|
37
|
+
needs: test
|
|
38
|
+
runs-on: ubuntu-latest
|
|
39
|
+
steps:
|
|
40
|
+
- uses: actions/checkout@v7
|
|
41
|
+
- uses: docker/setup-buildx-action@v4
|
|
42
|
+
- uses: docker/build-push-action@v7
|
|
43
|
+
with:
|
|
44
|
+
context: .
|
|
45
|
+
push: false
|
|
46
|
+
tags: app:ci
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
name: '{{projectName}}'
|
|
2
|
+
|
|
3
|
+
services:
|
|
4
|
+
app:
|
|
5
|
+
build: .
|
|
6
|
+
env_file: .env
|
|
7
|
+
environment:
|
|
8
|
+
NODE_ENV: production
|
|
9
|
+
PORT: 3000
|
|
10
|
+
DATABASE_URL: postgresql://postgres:postgres@db:5432/{{databaseName}}
|
|
11
|
+
depends_on:
|
|
12
|
+
db:
|
|
13
|
+
condition: service_healthy
|
|
14
|
+
ports:
|
|
15
|
+
- '${PORT:-3000}:3000'
|
|
16
|
+
|
|
17
|
+
db:
|
|
18
|
+
image: postgres:17-alpine
|
|
19
|
+
environment:
|
|
20
|
+
POSTGRES_USER: postgres
|
|
21
|
+
POSTGRES_PASSWORD: postgres
|
|
22
|
+
POSTGRES_DB: '{{databaseName}}'
|
|
23
|
+
ports:
|
|
24
|
+
- '${POSTGRES_PORT:-5432}:5432'
|
|
25
|
+
volumes:
|
|
26
|
+
- pgdata:/var/lib/postgresql/data
|
|
27
|
+
healthcheck:
|
|
28
|
+
test: ['CMD-SHELL', 'pg_isready -U postgres']
|
|
29
|
+
interval: 5s
|
|
30
|
+
timeout: 5s
|
|
31
|
+
retries: 10
|
|
32
|
+
|
|
33
|
+
volumes:
|
|
34
|
+
pgdata:
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"scripts": {
|
|
3
|
+
"db:migrate": "prisma migrate dev",
|
|
4
|
+
"db:deploy": "prisma migrate deploy",
|
|
5
|
+
"db:generate": "prisma generate",
|
|
6
|
+
"db:studio": "prisma studio",
|
|
7
|
+
"postinstall": "prisma generate"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@prisma/adapter-pg": "^7.10.1",
|
|
11
|
+
"@prisma/client": "^7.10.1",
|
|
12
|
+
"pg": "^8.16.3",
|
|
13
|
+
"prisma": "^7.10.1"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
-- CreateEnum
|
|
2
|
+
CREATE TYPE "Role" AS ENUM ('user', 'admin');
|
|
3
|
+
|
|
4
|
+
-- CreateTable
|
|
5
|
+
CREATE TABLE "users" (
|
|
6
|
+
"id" UUID NOT NULL,
|
|
7
|
+
"name" VARCHAR(50) NOT NULL,
|
|
8
|
+
"email" VARCHAR(254) NOT NULL,
|
|
9
|
+
"password" TEXT NOT NULL,
|
|
10
|
+
"role" "Role" NOT NULL DEFAULT 'user',
|
|
11
|
+
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
12
|
+
"updatedAt" TIMESTAMP(3) NOT NULL,
|
|
13
|
+
|
|
14
|
+
CONSTRAINT "users_pkey" PRIMARY KEY ("id")
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
-- CreateTable
|
|
18
|
+
CREATE TABLE "refresh_tokens" (
|
|
19
|
+
"id" UUID NOT NULL,
|
|
20
|
+
"userId" UUID NOT NULL,
|
|
21
|
+
"tokenHash" CHAR(64) NOT NULL,
|
|
22
|
+
"expiresAt" TIMESTAMP(3) NOT NULL,
|
|
23
|
+
"revokedAt" TIMESTAMP(3),
|
|
24
|
+
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
25
|
+
|
|
26
|
+
CONSTRAINT "refresh_tokens_pkey" PRIMARY KEY ("id")
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
-- CreateIndex
|
|
30
|
+
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");
|
|
31
|
+
|
|
32
|
+
-- CreateIndex
|
|
33
|
+
CREATE UNIQUE INDEX "refresh_tokens_tokenHash_key" ON "refresh_tokens"("tokenHash");
|
|
34
|
+
|
|
35
|
+
-- CreateIndex
|
|
36
|
+
CREATE INDEX "refresh_tokens_userId_idx" ON "refresh_tokens"("userId");
|
|
37
|
+
|
|
38
|
+
-- AddForeignKey
|
|
39
|
+
ALTER TABLE "refresh_tokens" ADD CONSTRAINT "refresh_tokens_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
generator client {
|
|
2
|
+
provider = "prisma-client"
|
|
3
|
+
output = "../src/generated/prisma"
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
datasource db {
|
|
7
|
+
provider = "postgresql"
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
enum Role {
|
|
11
|
+
user
|
|
12
|
+
admin
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
model User {
|
|
16
|
+
id String @id @default(uuid()) @db.Uuid
|
|
17
|
+
name String @db.VarChar(50)
|
|
18
|
+
email String @unique @db.VarChar(254)
|
|
19
|
+
password String
|
|
20
|
+
role Role @default(user)
|
|
21
|
+
createdAt DateTime @default(now())
|
|
22
|
+
updatedAt DateTime @updatedAt
|
|
23
|
+
refreshTokens RefreshToken[]
|
|
24
|
+
|
|
25
|
+
@@map("users")
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
model RefreshToken {
|
|
29
|
+
id String @id @default(uuid()) @db.Uuid
|
|
30
|
+
userId String @db.Uuid
|
|
31
|
+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
|
32
|
+
tokenHash String @unique @db.Char(64)
|
|
33
|
+
expiresAt DateTime
|
|
34
|
+
revokedAt DateTime?
|
|
35
|
+
createdAt DateTime @default(now())
|
|
36
|
+
|
|
37
|
+
@@index([userId])
|
|
38
|
+
@@map("refresh_tokens")
|
|
39
|
+
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|