Skip to content

Commit dfb66bb

Browse files
V0 (#33)
* feat: check user roles * refactor: nit * test: ensure and admin can assign and unassign a task * fix: authorization plugin has no dependency * fix: update migrations dir path * fix: eslint * refactor: nit * Update .env.example Signed-off-by: Jean <[email protected]> * refactor: use knex * refactor: migrations * fix: remove useless c8 ignore comments * docs: update path * refactor: change JWT auth for cookie session auth * chore: ci - env must have required property 'COOKIE_NAME' * fix: uncomment unauthenticated test * refactor: leverage fastify sensible decorators * chore: use tsx * feat: add pagination to tasks * refactor: use COUNT(*) OVER() AS rowNum for tasks pagination * refactor: decorate request for authorization * fix: use transaction for login controller * refactor: register cookie plugin in session plugin * test: mock app.compare implementation instead of reassignation * test: spy logger to ensure 500 error is due to Transaction failure * feat: allow to upload task image * refactor: improve scripts typing * docs: static and multipart plugin * chore: dangerous DB operations should be explicitly authorized * refactor: use node test runner utitities * refactor: check file size before mime-type * fix: identifier typo * feat: do not use rm -rf * fix: storage path disclosure * fix: nit --------- Signed-off-by: Jean <[email protected]>
1 parent 8ae2437 commit dfb66bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1209
-519
lines changed

.env.example

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
# @see {@link https://www.youtube.com/watch?v=HMM7GJC5E2o}
33
NODE_ENV=production
44

5+
CAN_CREATE_DATABASE=0
6+
CAN_DROP_DATABASE=0
7+
CAN_SEED_DATABASE=0
8+
59
# Database
610
MYSQL_HOST=localhost
711
MYSQL_PORT=3306
@@ -14,5 +18,6 @@ FASTIFY_CLOSE_GRACE_DELAY=1000
1418
LOG_LEVEL=info
1519

1620
# Security
17-
JWT_SECRET=
18-
RATE_LIMIT_MAX=
21+
COOKIE_SECRET=
22+
COOKIE_NAME=
23+
RATE_LIMIT_MAX=4 # 4 for tests

.github/workflows/ci.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99
paths-ignore:
1010
- "docs/**"
1111
- "*.md"
12+
- "*.example"
1213
pull_request:
1314
paths-ignore:
1415
- "docs/**"
@@ -50,11 +51,10 @@ jobs:
5051
- name: Lint Code
5152
run: npm run lint
5253

53-
- name: Generate JWT Secret
54-
id: gen-jwt
54+
- name: Generate COOKIE Secret
5555
run: |
56-
JWT_SECRET=$(openssl rand -hex 32)
57-
echo "JWT_SECRET=$JWT_SECRET" >> $GITHUB_ENV
56+
COOKIE_SECRET=$(openssl rand -hex 32)
57+
echo "COOKIE_SECRET=$COOKIE_SECRET" >> $GITHUB_ENV
5858
5959
- name: Generate dummy .env for scripts using -env-file=.env flag
6060
run: touch .env
@@ -66,6 +66,8 @@ jobs:
6666
MYSQL_DATABASE: test_db
6767
MYSQL_USER: test_user
6868
MYSQL_PASSWORD: test_password
69-
# JWT_SECRET is dynamically generated and loaded from the environment
69+
# COOKIE_SECRET is dynamically generated and loaded from the environment
70+
COOKIE_NAME: 'sessid'
7071
RATE_LIMIT_MAX: 4
72+
CAN_SEED_DATABASE: 1
7173
run: npm run db:migrate && npm run test

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,6 @@ bun.lockb
137137
package-lock.json
138138
pnpm-lock.yaml
139139
yarn.lock
140+
141+
# uploaded files
142+
uploads/tasks/*

@types/fastify/fastify.d.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
![CI](https://github.com/fastify/demo/workflows/CI/badge.svg)
44

5-
> :warning: **Please note:** This repository is still under active development.
6-
75
The aim of this repository is to provide a concrete example of a Fastify application using what are considered best practices by the Fastify community.
86

97
**Prerequisites:** You need to have Node.js version 22 or higher installed.

docker-compose.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ services:
22
db:
33
image: mysql:8.4
44
environment:
5-
MYSQL_DATABASE: ${MYSQL_DATABASE}
6-
MYSQL_USER: ${MYSQL_USER}
7-
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
5+
MYSQL_ROOT_PASSWORD: root_password
6+
MYSQL_DATABASE: ${MYSQL_DATABASE}
7+
MYSQL_USER: ${MYSQL_USER}
8+
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
89
ports:
910
- 3306:3306
11+
healthcheck:
12+
test: ["CMD", "mysqladmin", "ping", "-u${MYSQL_USER}", "-p${MYSQL_PASSWORD}"]
13+
interval: 10s
14+
timeout: 5s
15+
retries: 3
1016
volumes:
1117
- db_data:/var/lib/mysql
12-
18+
1319
volumes:
1420
db_data:

migrations/002.do.tasks.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CREATE TABLE tasks (
33
name VARCHAR(255) NOT NULL,
44
author_id INT NOT NULL,
55
assigned_user_id INT,
6+
filename VARCHAR(255),
67
status VARCHAR(50) NOT NULL,
78
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
89
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

migrations/004.do.roles.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE roles (
2+
id INT AUTO_INCREMENT PRIMARY KEY,
3+
name VARCHAR(255) NOT NULL
4+
);

migrations/004.undo.roles.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE IF EXISTS roles;

migrations/005.do.user_roles.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE user_roles (
2+
id INT AUTO_INCREMENT PRIMARY KEY,
3+
user_id INT NOT NULL,
4+
role_id INT NOT NULL,
5+
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
6+
FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE CASCADE
7+
);

0 commit comments

Comments
 (0)