Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
12dcabccb3 | |||
224d3b3764 | |||
fdecf5ce1a | |||
6926132a1b | |||
e1543becc5 | |||
b985172cd0 | |||
8ac1696ca0 | |||
de34618a7c | |||
400dc7ec2a | |||
49ac4f6ca4 | |||
8e69511e3e | |||
7e305429b4 | |||
b71da7dcc9 | |||
a6dd112e4a | |||
ab94d1e656 | |||
8483cd4772 | |||
46745e1b7e | |||
50dbab7dfe | |||
2f78604116 | |||
4d565e4f1f | |||
183377afc3 | |||
39989eb481 | |||
b13bd6a85e |
@ -1 +1,7 @@
|
||||
{ "extends": ["@commitlint/config-conventional"] }
|
||||
{
|
||||
"extends": ["@commitlint/config-conventional"],
|
||||
"rules": {
|
||||
"body-max-length": [0, "always"],
|
||||
"body-max-line-length": [0, "always"]
|
||||
}
|
||||
}
|
||||
|
@ -1,2 +1 @@
|
||||
ARG VARIANT="16"
|
||||
FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:0-${VARIANT}
|
||||
FROM mcr.microsoft.com/devcontainers/javascript-node:18
|
||||
|
@ -12,7 +12,7 @@ services:
|
||||
- 'host.docker.internal:host-gateway'
|
||||
|
||||
thream-database:
|
||||
image: 'postgres:14.2'
|
||||
image: 'postgres:15.0'
|
||||
environment:
|
||||
POSTGRES_USER: 'user'
|
||||
POSTGRES_PASSWORD: 'password'
|
||||
|
@ -15,6 +15,7 @@ GOOGLE_CLIENT_ID=
|
||||
GOOGLE_CLIENT_SECRET=
|
||||
HOST=0.0.0.0
|
||||
JWT_ACCESS_EXPIRES_IN=15 minutes
|
||||
# You can generate JWT secrets with the `npm run generate:jwt-secret` command.
|
||||
JWT_ACCESS_SECRET=accessTokenSecret
|
||||
JWT_REFRESH_SECRET=refreshTokenSecret
|
||||
NODE_ENV=development
|
||||
|
2
.github/workflows/analyze.yml
vendored
2
.github/workflows/analyze.yml
vendored
@ -19,7 +19,7 @@ jobs:
|
||||
- uses: 'actions/checkout@v3.0.0'
|
||||
|
||||
- name: 'Initialize CodeQL'
|
||||
uses: 'github/codeql-action/init@v1'
|
||||
uses: 'github/codeql-action/init@v2'
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
|
||||
|
6
.github/workflows/build.yml
vendored
6
.github/workflows/build.yml
vendored
@ -10,12 +10,12 @@ jobs:
|
||||
build:
|
||||
runs-on: 'ubuntu-latest'
|
||||
steps:
|
||||
- uses: 'actions/checkout@v3.0.0'
|
||||
- uses: 'actions/checkout@v3.1.0'
|
||||
|
||||
- name: 'Use Node.js'
|
||||
uses: 'actions/setup-node@v3.0.0'
|
||||
uses: 'actions/setup-node@v3.5.1'
|
||||
with:
|
||||
node-version: '16.x'
|
||||
node-version: '18.x'
|
||||
cache: 'npm'
|
||||
|
||||
- name: 'Install'
|
||||
|
9
.github/workflows/lint.yml
vendored
9
.github/workflows/lint.yml
vendored
@ -10,12 +10,12 @@ jobs:
|
||||
lint:
|
||||
runs-on: 'ubuntu-latest'
|
||||
steps:
|
||||
- uses: 'actions/checkout@v3.0.0'
|
||||
- uses: 'actions/checkout@v3.1.0'
|
||||
|
||||
- name: 'Use Node.js'
|
||||
uses: 'actions/setup-node@v3.0.0'
|
||||
uses: 'actions/setup-node@v3.5.1'
|
||||
with:
|
||||
node-version: '16.x'
|
||||
node-version: '18.x'
|
||||
cache: 'npm'
|
||||
|
||||
- name: 'Install'
|
||||
@ -45,3 +45,6 @@ jobs:
|
||||
uses: 'hadolint/hadolint-action@v1.6.0'
|
||||
with:
|
||||
dockerfile: './Dockerfile'
|
||||
|
||||
- name: 'prisma:validate'
|
||||
run: 'cp .env.example .env && npm run prisma:validate'
|
||||
|
6
.github/workflows/release.yml
vendored
6
.github/workflows/release.yml
vendored
@ -8,7 +8,7 @@ jobs:
|
||||
release:
|
||||
runs-on: 'ubuntu-latest'
|
||||
steps:
|
||||
- uses: 'actions/checkout@v3.0.0'
|
||||
- uses: 'actions/checkout@v3.1.0'
|
||||
with:
|
||||
fetch-depth: 0
|
||||
persist-credentials: false
|
||||
@ -21,9 +21,9 @@ jobs:
|
||||
git-commit-gpgsign: true
|
||||
|
||||
- name: 'Use Node.js'
|
||||
uses: 'actions/setup-node@v3.0.0'
|
||||
uses: 'actions/setup-node@v3.5.1'
|
||||
with:
|
||||
node-version: '16.x'
|
||||
node-version: '18.x'
|
||||
cache: 'npm'
|
||||
|
||||
- name: 'Install'
|
||||
|
8
.github/workflows/test.yml
vendored
8
.github/workflows/test.yml
vendored
@ -8,14 +8,14 @@ on:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: 'macos-latest'
|
||||
runs-on: 'ubuntu-latest'
|
||||
steps:
|
||||
- uses: 'actions/checkout@v3.0.0'
|
||||
- uses: 'actions/checkout@v3.1.0'
|
||||
|
||||
- name: 'Use Node.js'
|
||||
uses: 'actions/setup-node@v3.0.0'
|
||||
uses: 'actions/setup-node@v3.5.1'
|
||||
with:
|
||||
node-version: '16.x'
|
||||
node-version: '18.x'
|
||||
cache: 'npm'
|
||||
|
||||
- name: 'Install'
|
||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -35,3 +35,4 @@ npm-debug.log*
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
*.hbs
|
||||
|
@ -2,5 +2,6 @@
|
||||
"*": ["editorconfig-checker"],
|
||||
"*.{js,jsx,ts,tsx}": ["prettier --write", "eslint --fix"],
|
||||
"*.{json,jsonc,yml,yaml}": ["prettier --write"],
|
||||
"*.md": ["prettier --write", "markdownlint --dot --fix"]
|
||||
"*.{md,mdx}": ["prettier --write", "markdownlint-cli2 --fix"],
|
||||
"prisma/schema.prisma": ["prisma validate"]
|
||||
}
|
||||
|
10
.markdownlint-cli2.jsonc
Normal file
10
.markdownlint-cli2.jsonc
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"config": {
|
||||
"default": true,
|
||||
"MD013": false,
|
||||
"MD033": false,
|
||||
"MD041": false
|
||||
},
|
||||
"globs": ["**/*.{md,mdx}"],
|
||||
"ignores": ["**/node_modules"]
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
{
|
||||
"default": true,
|
||||
"MD013": false,
|
||||
"MD033": false,
|
||||
"MD041": false
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
build
|
||||
node_modules
|
||||
coverage
|
||||
package.json
|
||||
package-lock.json
|
||||
*.hbs
|
14
.swcrc
14
.swcrc
@ -2,21 +2,11 @@
|
||||
"jsc": {
|
||||
"parser": {
|
||||
"syntax": "typescript",
|
||||
"decorators": true,
|
||||
"dynamicImport": true
|
||||
},
|
||||
"transform": {
|
||||
"legacyDecorator": true,
|
||||
"decoratorMetadata": true
|
||||
},
|
||||
"target": "es2022",
|
||||
"loose": true
|
||||
"target": "es2022"
|
||||
},
|
||||
"module": {
|
||||
"type": "es6",
|
||||
"strict": false,
|
||||
"strictMode": true,
|
||||
"lazy": false,
|
||||
"noInterop": false
|
||||
"type": "es6"
|
||||
}
|
||||
}
|
||||
|
@ -58,9 +58,9 @@ Scopes define what part of the code changed.
|
||||
### Examples
|
||||
|
||||
```sh
|
||||
git commit -m "feat(services): add POST /users/signup"
|
||||
git commit -m "feat: add POST /users/signup"
|
||||
git commit -m "docs(readme): update installation process"
|
||||
git commit -m "fix(services): should emit events to connected users"
|
||||
git commit -m "fix: should emit events to connected users"
|
||||
```
|
||||
|
||||
## Directory Structure
|
||||
@ -86,7 +86,6 @@ git commit -m "fix(services): should emit events to connected users"
|
||||
- `services` : all REST API endpoints
|
||||
- `tools` : configs and utilities
|
||||
- `typings` : types gloablly used in the project
|
||||
- `uploads` : uploaded files by users
|
||||
|
||||
### Services folder explained with an example
|
||||
|
||||
@ -121,3 +120,5 @@ The folders after `src/services` : is the real path of the routes in the API exc
|
||||
folders starting and ending with `__` like `__test__` or `__utils__`.
|
||||
|
||||
The filenames correspond to the HTTP methods used (`get`, `post`, `put`, `delete`).
|
||||
|
||||
You can generate the boilerplate code for a new service with the `npm run generate` command.
|
||||
|
10
Dockerfile
10
Dockerfile
@ -1,23 +1,21 @@
|
||||
FROM node:16.14.2 AS dependencies
|
||||
FROM node:18.12.1 AS dependencies
|
||||
WORKDIR /usr/src/app
|
||||
COPY ./package*.json ./
|
||||
RUN npm install
|
||||
|
||||
FROM node:16.14.2 AS builder
|
||||
FROM node:18.12.1 AS builder
|
||||
WORKDIR /usr/src/app
|
||||
COPY --from=dependencies /usr/src/app/node_modules ./node_modules
|
||||
COPY ./ ./
|
||||
RUN npm run prisma:generate && npm run build
|
||||
|
||||
FROM node:16.14.2 AS runner
|
||||
FROM node:18.12.1 AS runner
|
||||
WORKDIR /usr/src/app
|
||||
ENV NODE_ENV=production
|
||||
COPY --from=builder /usr/src/app/node_modules ./node_modules
|
||||
COPY --from=builder /usr/src/app/start.sh ./docker-start.sh
|
||||
COPY --from=builder /usr/src/app/package.json ./package.json
|
||||
COPY --from=builder /usr/src/app/email ./email
|
||||
COPY --from=builder /usr/src/app/build ./build
|
||||
COPY --from=builder /usr/src/app/prisma ./prisma
|
||||
COPY --from=builder /usr/src/app/uploads ./uploads
|
||||
USER node
|
||||
CMD ["./docker-start.sh"]
|
||||
CMD ["node", "build/index.js"]
|
||||
|
16
README.md
16
README.md
@ -18,7 +18,7 @@
|
||||
|
||||
Thream's Application Programming Interface (API) to stay close with your friends and communities.
|
||||
|
||||
It uses [Thream/file-uploads-api](https://github.com/Thream/file-uploads-api) [v1.0.0](https://github.com/Thream/file-uploads-api/releases/tag/v1.0.0).
|
||||
It uses [Thream/file-uploads-api](https://github.com/Thream/file-uploads-api) [v1.1.0](https://github.com/Thream/file-uploads-api/releases/tag/v1.1.0).
|
||||
|
||||
## ⚙️ Getting Started
|
||||
|
||||
@ -49,6 +49,8 @@ the root of the project (see `.env.example`).
|
||||
|
||||
### Local Development environment
|
||||
|
||||
Recommended to use [VSCode: Remote development in Containers](https://code.visualstudio.com/docs/remote/containers-tutorial).
|
||||
|
||||
#### Setup the database
|
||||
|
||||
```sh
|
||||
@ -76,18 +78,6 @@ npm run dev
|
||||
npm run prisma:studio
|
||||
```
|
||||
|
||||
### Production environment with [Docker](https://www.docker.com/)
|
||||
|
||||
```sh
|
||||
# Setup and run all the services for you
|
||||
docker-compose up --build
|
||||
```
|
||||
|
||||
#### Services started
|
||||
|
||||
- API : `http://localhost:8080`
|
||||
- [PostgreSQL database](https://www.postgresql.org/)
|
||||
|
||||
## 💡 Contributing
|
||||
|
||||
Anyone can help to improve the project, submit a Feature Request, a bug report or
|
||||
|
@ -1,29 +0,0 @@
|
||||
version: '3.0'
|
||||
services:
|
||||
thream-api:
|
||||
container_name: ${COMPOSE_PROJECT_NAME}
|
||||
build:
|
||||
context: './'
|
||||
env_file:
|
||||
- '.env'
|
||||
ports:
|
||||
- '${PORT}:${PORT}'
|
||||
depends_on:
|
||||
- 'thream-database'
|
||||
volumes:
|
||||
- './uploads:/usr/src/app/uploads'
|
||||
restart: 'unless-stopped'
|
||||
|
||||
thream-database:
|
||||
container_name: 'thream-database'
|
||||
image: 'postgres:14.2'
|
||||
environment:
|
||||
POSTGRES_USER: 'user'
|
||||
POSTGRES_PASSWORD: 'password'
|
||||
POSTGRES_DB: 'thream'
|
||||
volumes:
|
||||
- 'database-volume:/var/lib/postgresql/data'
|
||||
restart: 'unless-stopped'
|
||||
|
||||
volumes:
|
||||
database-volume:
|
@ -1,4 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
npm run prisma:migrate:deploy
|
||||
node build/index.js
|
@ -22,7 +22,7 @@ export const serviceGenerator = {
|
||||
type: 'list',
|
||||
name: 'tag',
|
||||
message: 'tag',
|
||||
choices: ['users', 'guilds', 'channels', 'messages', 'members', 'uploads']
|
||||
choices: ['users', 'oauth2', 'guilds', 'channels', 'messages', 'members']
|
||||
},
|
||||
{
|
||||
type: 'confirm',
|
||||
|
10667
package-lock.json
generated
10667
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
98
package.json
98
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@thream/api",
|
||||
"version": "1.0.0",
|
||||
"version": "1.2.3",
|
||||
"description": "Thream's application programming interface to stay close with your friends and communities.",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
@ -18,13 +18,15 @@
|
||||
"start": "node build/index.js",
|
||||
"dev": "concurrently -k -n \"TypeScript,Node\" -p \"[{name}]\" -c \"blue,green\" \"npm run build:dev\" \"cross-env NODE_ENV=development nodemon build/index.js\"",
|
||||
"generate": "plop",
|
||||
"generate:jwt-secret": "node ./build/scripts/generate-jwt-secret.js",
|
||||
"lint:commit": "commitlint",
|
||||
"lint:editorconfig": "editorconfig-checker",
|
||||
"lint:markdown": "markdownlint \"**/*.md\" --dot --ignore-path \".gitignore\"",
|
||||
"lint:markdown": "markdownlint-cli2",
|
||||
"lint:typescript": "eslint \"**/*.{js,jsx,ts,tsx}\" --ignore-path \".gitignore\"",
|
||||
"lint:prettier": "prettier \".\" --check",
|
||||
"lint:prettier": "prettier \".\" --check --ignore-path \".gitignore\"",
|
||||
"lint:staged": "lint-staged",
|
||||
"test": "cross-env NODE_ENV=test c8 tap",
|
||||
"prisma:validate": "prisma validate",
|
||||
"prisma:generate": "prisma generate",
|
||||
"prisma:studio": "prisma studio",
|
||||
"prisma:migrate:dev": "prisma migrate dev",
|
||||
@ -33,69 +35,71 @@
|
||||
"postinstall": "husky install"
|
||||
},
|
||||
"dependencies": {
|
||||
"@prisma/client": "3.12.0",
|
||||
"@sinclair/typebox": "0.23.4",
|
||||
"@thream/socketio-jwt": "3.0.0",
|
||||
"axios": "0.26.1",
|
||||
"@fastify/cors": "8.2.0",
|
||||
"@fastify/helmet": "10.1.0",
|
||||
"@fastify/multipart": "7.3.0",
|
||||
"@fastify/rate-limit": "7.6.0",
|
||||
"@fastify/sensible": "5.2.0",
|
||||
"@fastify/swagger": "8.2.1",
|
||||
"@fastify/swagger-ui": "1.3.0",
|
||||
"@prisma/client": "4.5.0",
|
||||
"@sinclair/typebox": "0.25.13",
|
||||
"@thream/socketio-jwt": "3.0.1",
|
||||
"axios": "1.2.1",
|
||||
"bcryptjs": "2.4.3",
|
||||
"dotenv": "16.0.0",
|
||||
"ejs": "3.1.6",
|
||||
"fastify": "3.28.0",
|
||||
"fastify-cors": "6.0.3",
|
||||
"fastify-helmet": "7.0.1",
|
||||
"fastify-multipart": "5.3.1",
|
||||
"fastify-plugin": "3.0.1",
|
||||
"fastify-rate-limit": "5.8.0",
|
||||
"fastify-sensible": "3.1.2",
|
||||
"fastify-swagger": "5.1.0",
|
||||
"dotenv": "16.0.3",
|
||||
"ejs": "3.1.8",
|
||||
"fastify": "4.10.2",
|
||||
"fastify-plugin": "4.4.0",
|
||||
"form-data": "4.0.0",
|
||||
"http-errors": "2.0.0",
|
||||
"jsonwebtoken": "8.5.1",
|
||||
"ms": "2.1.3",
|
||||
"nodemailer": "6.7.3",
|
||||
"nodemailer": "6.8.0",
|
||||
"read-pkg": "7.1.0",
|
||||
"socket.io": "4.4.1"
|
||||
"socket.io": "4.5.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@commitlint/cli": "16.2.3",
|
||||
"@commitlint/config-conventional": "16.2.1",
|
||||
"@commitlint/cli": "17.2.0",
|
||||
"@commitlint/config-conventional": "17.2.0",
|
||||
"@saithodev/semantic-release-backmerge": "2.1.2",
|
||||
"@semantic-release/git": "10.0.1",
|
||||
"@swc/cli": "0.1.57",
|
||||
"@swc/core": "1.2.164",
|
||||
"@swc/core": "1.3.14",
|
||||
"@types/bcryptjs": "2.4.2",
|
||||
"@types/busboy": "1.5.0",
|
||||
"@types/ejs": "3.1.0",
|
||||
"@types/ejs": "3.1.1",
|
||||
"@types/http-errors": "1.8.2",
|
||||
"@types/jsonwebtoken": "8.5.8",
|
||||
"@types/jsonwebtoken": "8.5.9",
|
||||
"@types/ms": "0.7.31",
|
||||
"@types/node": "17.0.23",
|
||||
"@types/nodemailer": "6.4.4",
|
||||
"@types/sinon": "10.0.11",
|
||||
"@types/tap": "15.0.6",
|
||||
"@typescript-eslint/eslint-plugin": "5.18.0",
|
||||
"c8": "7.11.0",
|
||||
"concurrently": "7.1.0",
|
||||
"@types/node": "18.11.14",
|
||||
"@types/nodemailer": "6.4.6",
|
||||
"@types/sinon": "10.0.13",
|
||||
"@types/tap": "15.0.7",
|
||||
"@typescript-eslint/eslint-plugin": "5.46.1",
|
||||
"@typescript-eslint/parser": "5.46.1",
|
||||
"c8": "7.12.0",
|
||||
"concurrently": "7.6.0",
|
||||
"cross-env": "7.0.3",
|
||||
"editorconfig-checker": "4.0.2",
|
||||
"eslint": "8.12.0",
|
||||
"eslint-config-conventions": "2.0.0",
|
||||
"eslint": "8.29.0",
|
||||
"eslint-config-conventions": "6.0.0",
|
||||
"eslint-config-prettier": "8.5.0",
|
||||
"eslint-plugin-import": "2.26.0",
|
||||
"eslint-plugin-prettier": "4.0.0",
|
||||
"eslint-plugin-promise": "6.0.0",
|
||||
"eslint-plugin-unicorn": "42.0.0",
|
||||
"husky": "7.0.4",
|
||||
"lint-staged": "12.3.7",
|
||||
"markdownlint-cli": "0.31.1",
|
||||
"nodemon": "2.0.15",
|
||||
"plop": "3.0.5",
|
||||
"prettier": "2.6.2",
|
||||
"prisma": "3.12.0",
|
||||
"eslint-plugin-prettier": "4.2.1",
|
||||
"eslint-plugin-promise": "6.1.1",
|
||||
"eslint-plugin-unicorn": "45.0.2",
|
||||
"husky": "8.0.2",
|
||||
"lint-staged": "13.1.0",
|
||||
"markdownlint-cli2": "0.5.1",
|
||||
"nodemon": "2.0.20",
|
||||
"plop": "3.1.1",
|
||||
"prettier": "2.8.1",
|
||||
"prisma": "4.5.0",
|
||||
"rimraf": "3.0.2",
|
||||
"semantic-release": "19.0.2",
|
||||
"sinon": "13.0.1",
|
||||
"tap": "16.0.1",
|
||||
"typescript": "4.6.3"
|
||||
"semantic-release": "19.0.5",
|
||||
"sinon": "14.0.2",
|
||||
"tap": "16.3.2",
|
||||
"typescript": "4.9.4"
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
import { User } from '@prisma/client'
|
||||
import type { User } from '@prisma/client'
|
||||
import sinon from 'sinon'
|
||||
|
||||
import { refreshTokenExample } from '../../models/RefreshToken.js'
|
||||
import { userExample, UserJWT } from '../../models/User.js'
|
||||
import type { UserJWT } from '../../models/User.js'
|
||||
import { userExample } from '../../models/User.js'
|
||||
import { userSettingsExample } from '../../models/UserSettings.js'
|
||||
import {
|
||||
generateAccessToken,
|
||||
|
@ -1,21 +1,27 @@
|
||||
import dotenv from 'dotenv'
|
||||
import fastify from 'fastify'
|
||||
import fastifyCors from 'fastify-cors'
|
||||
import fastifySwagger from 'fastify-swagger'
|
||||
import fastifyHelmet from 'fastify-helmet'
|
||||
import fastifyRateLimit from 'fastify-rate-limit'
|
||||
import fastifySensible from 'fastify-sensible'
|
||||
import fastifyCors from '@fastify/cors'
|
||||
import fastifySwagger from '@fastify/swagger'
|
||||
import fastifySwaggerUI from '@fastify/swagger-ui'
|
||||
import fastifyHelmet from '@fastify/helmet'
|
||||
import fastifyRateLimit from '@fastify/rate-limit'
|
||||
import fastifySensible from '@fastify/sensible'
|
||||
import { readPackage } from 'read-pkg'
|
||||
|
||||
import { services } from './services/index.js'
|
||||
import { swaggerOptions } from './tools/configurations/swaggerOptions.js'
|
||||
import fastifySocketIo from './tools/plugins/socket-io.js'
|
||||
|
||||
dotenv.config()
|
||||
const packageJSON = await readPackage()
|
||||
export const application = fastify({
|
||||
logger: process.env.NODE_ENV === 'development',
|
||||
ajv: {
|
||||
customOptions: {
|
||||
format: 'full'
|
||||
strict: 'log',
|
||||
keywords: ['kind', 'modifier'],
|
||||
formats: {
|
||||
full: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -35,5 +41,35 @@ await application.register(fastifyRateLimit, {
|
||||
max: 200,
|
||||
timeWindow: '1 minute'
|
||||
})
|
||||
await application.register(fastifySwagger, swaggerOptions)
|
||||
await application.register(fastifySwagger, {
|
||||
openapi: {
|
||||
info: {
|
||||
title: packageJSON.name,
|
||||
description: packageJSON.description,
|
||||
version: packageJSON.version
|
||||
},
|
||||
tags: [
|
||||
{ name: 'users' },
|
||||
{ name: 'oauth2' },
|
||||
{ name: 'guilds' },
|
||||
{ name: 'channels' },
|
||||
{ name: 'messages' },
|
||||
{ name: 'members' }
|
||||
],
|
||||
components: {
|
||||
securitySchemes: {
|
||||
bearerAuth: {
|
||||
type: 'http',
|
||||
scheme: 'bearer',
|
||||
bearerFormat: 'JWT'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
hideUntagged: true
|
||||
})
|
||||
await application.register(fastifySwaggerUI, {
|
||||
routePrefix: '/documentation',
|
||||
staticCSP: true
|
||||
})
|
||||
await application.register(services)
|
||||
|
@ -1,5 +1,8 @@
|
||||
import { application } from './application.js'
|
||||
import { HOST, PORT } from './tools/configurations/index.js'
|
||||
import { HOST, PORT } from './tools/configurations.js'
|
||||
|
||||
const address = await application.listen(PORT, HOST)
|
||||
const address = await application.listen({
|
||||
port: PORT,
|
||||
host: HOST
|
||||
})
|
||||
console.log('\u001B[36m%s\u001B[0m', `🚀 Server listening at ${address}`)
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import { Channel } from '@prisma/client'
|
||||
import type { Channel } from '@prisma/client'
|
||||
|
||||
import { date, id } from './utils.js'
|
||||
import { guildExample } from './Guild.js'
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Guild } from '@prisma/client'
|
||||
import type { Guild } from '@prisma/client'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
|
||||
import { date, id } from './utils.js'
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import { Member } from '@prisma/client'
|
||||
import type { Member } from '@prisma/client'
|
||||
|
||||
import { date, id } from './utils.js'
|
||||
import { guildExample } from './Guild.js'
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Message } from '@prisma/client'
|
||||
import type { Message } from '@prisma/client'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
|
||||
import { date, id } from './utils.js'
|
||||
@ -14,8 +14,7 @@ export const messageSchema = {
|
||||
type: Type.Union(types, { default: 'text' }),
|
||||
mimetype: Type.String({
|
||||
maxLength: 127,
|
||||
default: 'text/plain',
|
||||
format: 'mimetype'
|
||||
default: 'text/plain'
|
||||
}),
|
||||
createdAt: date.createdAt,
|
||||
updatedAt: date.updatedAt,
|
||||
|
@ -5,12 +5,12 @@ import { date, id } from './utils.js'
|
||||
export const providers = ['Google', 'GitHub', 'Discord'] as const
|
||||
export const strategies = [...providers, 'Local'] as const
|
||||
|
||||
export const strategiesTypebox = strategies.map((strategy) =>
|
||||
Type.Literal(strategy)
|
||||
)
|
||||
export const providersTypebox = providers.map((provider) =>
|
||||
Type.Literal(provider)
|
||||
)
|
||||
export const strategiesTypebox = strategies.map((strategy) => {
|
||||
return Type.Literal(strategy)
|
||||
})
|
||||
export const providersTypebox = providers.map((provider) => {
|
||||
return Type.Literal(provider)
|
||||
})
|
||||
|
||||
export type ProviderOAuth = typeof providers[number]
|
||||
export type AuthenticationStrategy = typeof strategies[number]
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { RefreshToken } from '@prisma/client'
|
||||
import type { RefreshToken } from '@prisma/client'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
|
||||
import { userExample } from './User.js'
|
||||
@ -6,7 +6,7 @@ import { date, id } from './utils.js'
|
||||
|
||||
export const refreshTokensSchema = {
|
||||
id,
|
||||
token: Type.String(),
|
||||
token: Type.String({ format: 'uuid' }),
|
||||
createdAt: date.createdAt,
|
||||
updatedAt: date.updatedAt,
|
||||
userId: id
|
||||
@ -15,7 +15,7 @@ export const refreshTokensSchema = {
|
||||
export const refreshTokenExample: RefreshToken = {
|
||||
id: 1,
|
||||
userId: userExample.id,
|
||||
token: 'sometoken',
|
||||
token: 'sometokenUUID',
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date()
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
import { User } from '@prisma/client'
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import type { User } from '@prisma/client'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
|
||||
import { AuthenticationStrategy, strategiesTypebox } from './OAuth.js'
|
||||
import type { AuthenticationStrategy } from './OAuth.js'
|
||||
import { strategiesTypebox } from './OAuth.js'
|
||||
import { userSettingsSchema } from './UserSettings.js'
|
||||
import { date, id } from './utils.js'
|
||||
|
||||
@ -10,6 +12,10 @@ export interface UserJWT {
|
||||
currentStrategy: AuthenticationStrategy
|
||||
}
|
||||
|
||||
export interface UserRefreshJWT extends UserJWT {
|
||||
tokenUUID: string
|
||||
}
|
||||
|
||||
export interface UserRequest {
|
||||
current: User
|
||||
currentStrategy: AuthenticationStrategy
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { UserSetting } from '@prisma/client'
|
||||
import { Type, Static } from '@sinclair/typebox'
|
||||
import type { UserSetting } from '@prisma/client'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
|
||||
import { date, id } from './utils.js'
|
||||
|
||||
@ -8,10 +9,10 @@ export const themes = [Type.Literal('light'), Type.Literal('dark')]
|
||||
|
||||
export const userSettingsSchema = {
|
||||
id,
|
||||
language: Type.Union(languages, { default: 'en' }),
|
||||
theme: Type.Union(themes, { default: 'dark' }),
|
||||
isPublicEmail: Type.Boolean({ default: false }),
|
||||
isPublicGuilds: Type.Boolean({ default: false }),
|
||||
language: Type.Union(languages),
|
||||
theme: Type.Union(themes),
|
||||
isPublicEmail: Type.Boolean(),
|
||||
isPublicGuilds: Type.Boolean(),
|
||||
createdAt: date.createdAt,
|
||||
updatedAt: date.updatedAt,
|
||||
userId: id
|
||||
|
@ -34,7 +34,7 @@ export const fastifyErrorsSchema = {
|
||||
404: {
|
||||
statusCode: Type.Literal(404),
|
||||
error: Type.Literal('Not Found'),
|
||||
message: Type.Literal('Not Found')
|
||||
message: Type.String()
|
||||
},
|
||||
431: {
|
||||
statusCode: Type.Literal(431),
|
||||
|
3
src/scripts/generate-jwt-secret.ts
Normal file
3
src/scripts/generate-jwt-secret.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import crypto from 'node:crypto'
|
||||
|
||||
console.log(crypto.randomBytes(256).toString('base64'))
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../../models/utils.js'
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../../models/utils.js'
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { Type, Static } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import fastifyMultipart from 'fastify-multipart'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import fastifyMultipart from '@fastify/multipart'
|
||||
|
||||
import prisma from '../../../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../../../models/utils.js'
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { FastifyPluginAsync } from 'fastify'
|
||||
import type { FastifyPluginAsync } from 'fastify'
|
||||
|
||||
import { deleteChannelService } from './[channelId]/delete.js'
|
||||
import { getChannelByIdService } from './[channelId]/get.js'
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../../models/utils.js'
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../../models/utils.js'
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import fastifyMultipart from 'fastify-multipart'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import fastifyMultipart from '@fastify/multipart'
|
||||
|
||||
import authenticateUser from '../../../../tools/plugins/authenticateUser.js'
|
||||
import { fastifyErrors } from '../../../../models/utils.js'
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../../models/utils.js'
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../../../tools/database/prisma.js'
|
||||
import {
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../../../models/utils.js'
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Type, Static } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../tools/database/prisma.js'
|
||||
import { fastifyErrors, id } from '../../models/utils.js'
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { FastifyPluginAsync } from 'fastify'
|
||||
import type { FastifyPluginAsync } from 'fastify'
|
||||
|
||||
import { getGuilds } from './get.js'
|
||||
import { postGuilds } from './post.js'
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../models/utils.js'
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
@ -18,7 +19,8 @@ const querySchema = Type.Object({
|
||||
export type QuerySchemaType = Static<typeof querySchema>
|
||||
|
||||
const getServiceSchema: FastifySchema = {
|
||||
description: 'GET all the public guilds.',
|
||||
description:
|
||||
'GET all the public guilds (ordered by descending members count).',
|
||||
tags: ['guilds'] as string[],
|
||||
security: [
|
||||
{
|
||||
@ -55,7 +57,11 @@ export const getGuildsPublic: FastifyPluginAsync = async (fastify) => {
|
||||
}
|
||||
const guildsRequest = await prisma.guild.findMany({
|
||||
...getPaginationOptions(request.query),
|
||||
orderBy: { createdAt: 'desc' },
|
||||
orderBy: {
|
||||
members: {
|
||||
_count: 'desc'
|
||||
}
|
||||
},
|
||||
...(request.query.search != null && {
|
||||
where: {
|
||||
name: { contains: request.query.search }
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { FastifyPluginAsync } from 'fastify'
|
||||
import type { FastifyPluginAsync } from 'fastify'
|
||||
|
||||
import { usersService } from './users/index.js'
|
||||
import { guildsService } from './guilds/index.js'
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { FastifyPluginAsync } from 'fastify'
|
||||
import type { FastifyPluginAsync } from 'fastify'
|
||||
|
||||
import { deleteMessageService } from './[messageId]/delete.js'
|
||||
import { putMessageService } from './[messageId]/put.js'
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
@ -50,6 +51,7 @@ export const getUserById: FastifyPluginAsync = async (fastify) => {
|
||||
id: true,
|
||||
name: true,
|
||||
email: settings.isPublicEmail,
|
||||
isConfirmed: true,
|
||||
logo: true,
|
||||
status: true,
|
||||
biography: true,
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import fastifyMultipart from 'fastify-multipart'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import fastifyMultipart from '@fastify/multipart'
|
||||
|
||||
import authenticateUser from '../../../../tools/plugins/authenticateUser.js'
|
||||
import { fastifyErrors } from '../../../../models/utils.js'
|
||||
|
@ -1,15 +1,16 @@
|
||||
import { randomUUID } from 'node:crypto'
|
||||
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
import authenticateUser from '../../../tools/plugins/authenticateUser.js'
|
||||
import { userCurrentSchema, userSchema } from '../../../models/User.js'
|
||||
import { sendEmail } from '../../../tools/email/sendEmail.js'
|
||||
import { API_URL } from '../../../tools/configurations/index.js'
|
||||
import { Language, Theme } from '../../../models/UserSettings.js'
|
||||
import { API_URL } from '../../../tools/configurations.js'
|
||||
import type { Language, Theme } from '../../../models/UserSettings.js'
|
||||
import { parseStringNullish } from '../../../tools/utils/parseStringNullish.js'
|
||||
|
||||
const bodyPutServiceSchema = Type.Object({
|
||||
@ -136,7 +137,9 @@ export const putCurrentUser: FastifyPluginAsync = async (fastify) => {
|
||||
})
|
||||
await fastify.io.emitToAuthorizedUsers({
|
||||
event: 'users',
|
||||
isAuthorizedCallback: () => true,
|
||||
isAuthorizedCallback: () => {
|
||||
return true
|
||||
},
|
||||
payload: {
|
||||
action: 'update',
|
||||
item: user
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../../models/utils.js'
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { FastifyPluginAsync } from 'fastify'
|
||||
import type { FastifyPluginAsync } from 'fastify'
|
||||
|
||||
import { postSignupUser } from './signup/post.js'
|
||||
import { getConfirmEmail } from './confirm-email/get.js'
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../../models/utils.js'
|
||||
@ -14,7 +15,7 @@ type Parameters = Static<typeof parametersSchema>
|
||||
|
||||
const deleteServiceSchema: FastifySchema = {
|
||||
description: 'DELETE a provider to authenticate with for a user.',
|
||||
tags: ['users'] as string[],
|
||||
tags: ['oauth2'] as string[],
|
||||
security: [
|
||||
{
|
||||
bearerAuth: []
|
||||
@ -55,7 +56,9 @@ export const deleteProviderService: FastifyPluginAsync = async (fastify) => {
|
||||
if (user.current.password != null) {
|
||||
strategies.push('Local')
|
||||
}
|
||||
const oauthProvider = OAuths.find((oauth) => oauth.provider === provider)
|
||||
const oauthProvider = OAuths.find((oauth) => {
|
||||
return oauth.provider === provider
|
||||
})
|
||||
if (oauthProvider == null) {
|
||||
throw fastify.httpErrors.notFound('You are not using this provider')
|
||||
}
|
||||
|
@ -1,7 +1,8 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import { API_URL } from '../../../../../tools/configurations/index.js'
|
||||
import { API_URL } from '../../../../../tools/configurations.js'
|
||||
import { fastifyErrors } from '../../../../../models/utils.js'
|
||||
import { DISCORD_BASE_URL, DISCORD_CLIENT_ID } from '../__utils__/utils.js'
|
||||
import authenticateUser from '../../../../../tools/plugins/authenticateUser.js'
|
||||
@ -14,7 +15,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
||||
|
||||
const getServiceSchema: FastifySchema = {
|
||||
description: 'Discord OAuth2 - add-strategy',
|
||||
tags: ['users'] as string[],
|
||||
tags: ['oauth2'] as string[],
|
||||
security: [
|
||||
{
|
||||
bearerAuth: []
|
||||
|
@ -1,7 +1,8 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import { API_URL } from '../../../../../tools/configurations/index.js'
|
||||
import { API_URL } from '../../../../../tools/configurations.js'
|
||||
import { fastifyErrors } from '../../../../../models/utils.js'
|
||||
import { discordStrategy, getDiscordUserData } from '../__utils__/utils.js'
|
||||
import { buildQueryURL } from '../../../../../tools/utils/buildQueryURL.js'
|
||||
@ -17,7 +18,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
||||
|
||||
const getServiceSchema: FastifySchema = {
|
||||
description: 'Discord OAuth2 - callback-add-strategy',
|
||||
tags: ['users'] as string[],
|
||||
tags: ['oauth2'] as string[],
|
||||
querystring: querySchema,
|
||||
response: {
|
||||
200: Type.String(),
|
||||
|
@ -1,7 +1,8 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import { API_URL } from '../../../../../tools/configurations/index.js'
|
||||
import { API_URL } from '../../../../../tools/configurations.js'
|
||||
import { fastifyErrors } from '../../../../../models/utils.js'
|
||||
import { discordStrategy, getDiscordUserData } from '../__utils__/utils.js'
|
||||
import { buildQueryURL } from '../../../../../tools/utils/buildQueryURL.js'
|
||||
@ -15,7 +16,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
||||
|
||||
const getServiceSchema: FastifySchema = {
|
||||
description: 'Discord OAuth2 - callback',
|
||||
tags: ['users'] as string[],
|
||||
tags: ['oauth2'] as string[],
|
||||
querystring: querySchema,
|
||||
response: {
|
||||
200: Type.String(),
|
||||
|
@ -1,7 +1,8 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import { API_URL } from '../../../../../tools/configurations/index.js'
|
||||
import { API_URL } from '../../../../../tools/configurations.js'
|
||||
import { fastifyErrors } from '../../../../../models/utils.js'
|
||||
import { DISCORD_BASE_URL, DISCORD_CLIENT_ID } from '../__utils__/utils.js'
|
||||
|
||||
@ -13,7 +14,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
||||
|
||||
const getServiceSchema: FastifySchema = {
|
||||
description: 'Discord OAuth2 - signin',
|
||||
tags: ['users'] as string[],
|
||||
tags: ['oauth2'] as string[],
|
||||
querystring: querySchema,
|
||||
response: {
|
||||
200: Type.String(),
|
||||
|
@ -1,7 +1,8 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import { API_URL } from '../../../../../tools/configurations/index.js'
|
||||
import { API_URL } from '../../../../../tools/configurations.js'
|
||||
import { fastifyErrors } from '../../../../../models/utils.js'
|
||||
import { GITHUB_BASE_URL, GITHUB_CLIENT_ID } from '../__utils__/utils.js'
|
||||
import authenticateUser from '../../../../../tools/plugins/authenticateUser.js'
|
||||
@ -14,7 +15,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
||||
|
||||
const getServiceSchema: FastifySchema = {
|
||||
description: 'GitHub OAuth2 - add-strategy',
|
||||
tags: ['users'] as string[],
|
||||
tags: ['oauth2'] as string[],
|
||||
security: [
|
||||
{
|
||||
bearerAuth: []
|
||||
|
@ -1,7 +1,8 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import { API_URL } from '../../../../../tools/configurations/index.js'
|
||||
import { API_URL } from '../../../../../tools/configurations.js'
|
||||
import { fastifyErrors } from '../../../../../models/utils.js'
|
||||
import { githubStrategy, getGitHubUserData } from '../__utils__/utils.js'
|
||||
import { buildQueryURL } from '../../../../../tools/utils/buildQueryURL.js'
|
||||
@ -17,7 +18,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
||||
|
||||
const getServiceSchema: FastifySchema = {
|
||||
description: 'GitHub OAuth2 - callback-add-strategy',
|
||||
tags: ['users'] as string[],
|
||||
tags: ['oauth2'] as string[],
|
||||
querystring: querySchema,
|
||||
response: {
|
||||
200: Type.String(),
|
||||
|
@ -1,7 +1,8 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import { API_URL } from '../../../../../tools/configurations/index.js'
|
||||
import { API_URL } from '../../../../../tools/configurations.js'
|
||||
import { fastifyErrors } from '../../../../../models/utils.js'
|
||||
import { githubStrategy, getGitHubUserData } from '../__utils__/utils.js'
|
||||
import { buildQueryURL } from '../../../../../tools/utils/buildQueryURL.js'
|
||||
@ -15,7 +16,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
||||
|
||||
const getServiceSchema: FastifySchema = {
|
||||
description: 'GitHub OAuth2 - callback',
|
||||
tags: ['users'] as string[],
|
||||
tags: ['oauth2'] as string[],
|
||||
querystring: querySchema,
|
||||
response: {
|
||||
200: Type.String(),
|
||||
|
@ -1,7 +1,8 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import { API_URL } from '../../../../../tools/configurations/index.js'
|
||||
import { API_URL } from '../../../../../tools/configurations.js'
|
||||
import { fastifyErrors } from '../../../../../models/utils.js'
|
||||
import { GITHUB_BASE_URL, GITHUB_CLIENT_ID } from '../__utils__/utils.js'
|
||||
|
||||
@ -13,7 +14,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
||||
|
||||
const getServiceSchema: FastifySchema = {
|
||||
description: 'GitHub OAuth2 - signin',
|
||||
tags: ['users'] as string[],
|
||||
tags: ['oauth2'] as string[],
|
||||
querystring: querySchema,
|
||||
response: {
|
||||
200: Type.String(),
|
||||
|
@ -1,7 +1,8 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import { API_URL } from '../../../../../tools/configurations/index.js'
|
||||
import { API_URL } from '../../../../../tools/configurations.js'
|
||||
import { fastifyErrors } from '../../../../../models/utils.js'
|
||||
import { GOOGLE_BASE_URL, GOOGLE_CLIENT_ID } from '../__utils__/utils.js'
|
||||
import authenticateUser from '../../../../../tools/plugins/authenticateUser.js'
|
||||
@ -14,7 +15,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
||||
|
||||
const getServiceSchema: FastifySchema = {
|
||||
description: 'Google OAuth2 - add-strategy',
|
||||
tags: ['users'] as string[],
|
||||
tags: ['oauth2'] as string[],
|
||||
security: [
|
||||
{
|
||||
bearerAuth: []
|
||||
|
@ -1,7 +1,8 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import { API_URL } from '../../../../../tools/configurations/index.js'
|
||||
import { API_URL } from '../../../../../tools/configurations.js'
|
||||
import { fastifyErrors } from '../../../../../models/utils.js'
|
||||
import { googleStrategy, getGoogleUserData } from '../__utils__/utils.js'
|
||||
import { buildQueryURL } from '../../../../../tools/utils/buildQueryURL.js'
|
||||
@ -17,7 +18,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
||||
|
||||
const getServiceSchema: FastifySchema = {
|
||||
description: 'Google OAuth2 - callback-add-strategy',
|
||||
tags: ['users'] as string[],
|
||||
tags: ['oauth2'] as string[],
|
||||
querystring: querySchema,
|
||||
response: {
|
||||
200: Type.String(),
|
||||
|
@ -1,7 +1,8 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import { API_URL } from '../../../../../tools/configurations/index.js'
|
||||
import { API_URL } from '../../../../../tools/configurations.js'
|
||||
import { fastifyErrors } from '../../../../../models/utils.js'
|
||||
import { googleStrategy, getGoogleUserData } from '../__utils__/utils.js'
|
||||
import { buildQueryURL } from '../../../../../tools/utils/buildQueryURL.js'
|
||||
@ -15,7 +16,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
||||
|
||||
const getServiceSchema: FastifySchema = {
|
||||
description: 'Google OAuth2 - callback',
|
||||
tags: ['users'] as string[],
|
||||
tags: ['oauth2'] as string[],
|
||||
querystring: querySchema,
|
||||
response: {
|
||||
200: Type.String(),
|
||||
|
@ -1,7 +1,8 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import { API_URL } from '../../../../../tools/configurations/index.js'
|
||||
import { API_URL } from '../../../../../tools/configurations.js'
|
||||
import { fastifyErrors } from '../../../../../models/utils.js'
|
||||
import { GOOGLE_BASE_URL, GOOGLE_CLIENT_ID } from '../__utils__/utils.js'
|
||||
|
||||
@ -13,7 +14,7 @@ type QuerySchemaType = Static<typeof querySchema>
|
||||
|
||||
const getServiceSchema: FastifySchema = {
|
||||
description: 'Google OAuth2 - signin',
|
||||
tags: ['users'] as string[],
|
||||
tags: ['oauth2'] as string[],
|
||||
querystring: querySchema,
|
||||
response: {
|
||||
200: Type.String(),
|
||||
|
@ -1,5 +1,6 @@
|
||||
import tap from 'tap'
|
||||
import sinon from 'sinon'
|
||||
import jwt from 'jsonwebtoken'
|
||||
|
||||
import { application } from '../../../../application.js'
|
||||
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
|
||||
@ -13,8 +14,7 @@ await tap.test('POST /users/refresh-token', async (t) => {
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const { accessToken, refreshToken, refreshTokenStubValue } =
|
||||
await authenticateUserTest()
|
||||
const { refreshToken, refreshTokenStubValue } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'refreshToken').value({
|
||||
...refreshTokenStubValue,
|
||||
findFirst: async () => {
|
||||
@ -28,9 +28,6 @@ await tap.test('POST /users/refresh-token', async (t) => {
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/refresh-token',
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
},
|
||||
payload: { refreshToken }
|
||||
})
|
||||
const responseJson = response.json()
|
||||
@ -62,6 +59,9 @@ await tap.test('POST /users/refresh-token', async (t) => {
|
||||
return refreshTokenExample
|
||||
}
|
||||
})
|
||||
sinon.stub(jwt, 'verify').value(() => {
|
||||
throw new Error('Invalid token')
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/refresh-token',
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import jwt from 'jsonwebtoken'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
@ -9,8 +10,8 @@ import {
|
||||
jwtSchema,
|
||||
expiresIn
|
||||
} from '../../../tools/utils/jwtToken.js'
|
||||
import { UserJWT } from '../../../models/User.js'
|
||||
import { JWT_REFRESH_SECRET } from '../../../tools/configurations/index.js'
|
||||
import type { UserRefreshJWT } from '../../../models/User.js'
|
||||
import { JWT_REFRESH_SECRET } from '../../../tools/configurations.js'
|
||||
|
||||
const bodyPostRefreshTokenSchema = Type.Object({
|
||||
refreshToken: jwtSchema.refreshToken
|
||||
@ -43,20 +44,20 @@ export const postRefreshTokenUser: FastifyPluginAsync = async (fastify) => {
|
||||
schema: postRefreshTokenSchema,
|
||||
handler: async (request, reply) => {
|
||||
const { refreshToken } = request.body
|
||||
try {
|
||||
const userRefreshJWT = jwt.verify(
|
||||
refreshToken,
|
||||
JWT_REFRESH_SECRET
|
||||
) as UserRefreshJWT
|
||||
const foundRefreshToken = await prisma.refreshToken.findFirst({
|
||||
where: { token: refreshToken }
|
||||
where: { token: userRefreshJWT.tokenUUID }
|
||||
})
|
||||
if (foundRefreshToken == null) {
|
||||
throw fastify.httpErrors.forbidden()
|
||||
}
|
||||
try {
|
||||
const userJWT = jwt.verify(
|
||||
foundRefreshToken.token,
|
||||
JWT_REFRESH_SECRET
|
||||
) as UserJWT
|
||||
const accessToken = generateAccessToken({
|
||||
id: userJWT.id,
|
||||
currentStrategy: userJWT.currentStrategy
|
||||
id: userRefreshJWT.id,
|
||||
currentStrategy: userRefreshJWT.currentStrategy
|
||||
})
|
||||
reply.statusCode = 200
|
||||
return {
|
||||
|
@ -25,6 +25,11 @@ await tap.test('PUT /users/reset-password', async (t) => {
|
||||
return userExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'refreshToken').value({
|
||||
deleteMany: async () => {
|
||||
return { count: 1 }
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: '/users/reset-password',
|
||||
|
@ -1,14 +1,15 @@
|
||||
import { randomUUID } from 'node:crypto'
|
||||
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import ms from 'ms'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
import { userSchema } from '../../../models/User.js'
|
||||
import { sendEmail } from '../../../tools/email/sendEmail.js'
|
||||
import { Language, Theme } from '../../../models/UserSettings.js'
|
||||
import type { Language, Theme } from '../../../models/UserSettings.js'
|
||||
|
||||
const queryPostResetPasswordSchema = Type.Object({
|
||||
redirectURI: Type.String({ format: 'uri-reference' })
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import bcrypt from 'bcryptjs'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
@ -39,7 +40,7 @@ export const putResetPasswordUser: FastifyPluginAsync = async (fastify) => {
|
||||
user?.temporaryExpirationToken != null &&
|
||||
user.temporaryExpirationToken.getTime() > Date.now()
|
||||
if (user == null || !isValidTemporaryToken) {
|
||||
throw fastify.httpErrors.badRequest('"tempToken" is invalid')
|
||||
throw fastify.httpErrors.badRequest('`temporaryToken` is invalid.')
|
||||
}
|
||||
const hashedPassword = await bcrypt.hash(password, 12)
|
||||
await prisma.user.update({
|
||||
@ -52,6 +53,11 @@ export const putResetPasswordUser: FastifyPluginAsync = async (fastify) => {
|
||||
temporaryExpirationToken: null
|
||||
}
|
||||
})
|
||||
await prisma.refreshToken.deleteMany({
|
||||
where: {
|
||||
userId: user.id
|
||||
}
|
||||
})
|
||||
reply.statusCode = 200
|
||||
return 'The new password has been saved!'
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import bcrypt from 'bcryptjs'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
|
@ -1,9 +1,11 @@
|
||||
import tap from 'tap'
|
||||
import sinon from 'sinon'
|
||||
import jwt from 'jsonwebtoken'
|
||||
|
||||
import { application } from '../../../../application.js'
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
import { refreshTokenExample } from '../../../../models/RefreshToken.js'
|
||||
import type { UserRefreshJWT } from '../../../../models/User.js'
|
||||
|
||||
await tap.test('POST /users/signout', async (t) => {
|
||||
t.afterEach(() => {
|
||||
@ -17,10 +19,18 @@ await tap.test('POST /users/signout', async (t) => {
|
||||
},
|
||||
delete: async () => {}
|
||||
})
|
||||
sinon.stub(jwt, 'verify').value(() => {
|
||||
const value: UserRefreshJWT = {
|
||||
id: 1,
|
||||
tokenUUID: refreshTokenExample.token,
|
||||
currentStrategy: 'Local'
|
||||
}
|
||||
return value
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/signout',
|
||||
payload: { refreshToken: refreshTokenExample.token }
|
||||
payload: { refreshToken: 'jwt token' }
|
||||
})
|
||||
t.equal(response.statusCode, 200)
|
||||
})
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
|
@ -1,12 +1,16 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import jwt from 'jsonwebtoken'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
import { refreshTokensSchema } from '../../../models/RefreshToken.js'
|
||||
import { JWT_REFRESH_SECRET } from '../../../tools/configurations.js'
|
||||
import type { UserRefreshJWT } from '../../../models/User.js'
|
||||
import { jwtSchema } from '../../../tools/utils/jwtToken.js'
|
||||
|
||||
const bodyPostSignoutSchema = Type.Object({
|
||||
refreshToken: refreshTokensSchema.token
|
||||
refreshToken: jwtSchema.refreshToken
|
||||
})
|
||||
|
||||
type BodyPostSignoutSchemaType = Static<typeof bodyPostSignoutSchema>
|
||||
@ -32,21 +36,27 @@ export const postSignoutUser: FastifyPluginAsync = async (fastify) => {
|
||||
schema: postSignoutSchema,
|
||||
handler: async (request, reply) => {
|
||||
const { refreshToken } = request.body
|
||||
const token = await prisma.refreshToken.findFirst({
|
||||
where: {
|
||||
token: refreshToken
|
||||
}
|
||||
try {
|
||||
const userRefreshJWT = jwt.verify(
|
||||
refreshToken,
|
||||
JWT_REFRESH_SECRET
|
||||
) as UserRefreshJWT
|
||||
const foundRefreshToken = await prisma.refreshToken.findFirst({
|
||||
where: { token: userRefreshJWT.tokenUUID }
|
||||
})
|
||||
if (token == null) {
|
||||
if (foundRefreshToken == null) {
|
||||
throw fastify.httpErrors.notFound()
|
||||
}
|
||||
await prisma.refreshToken.delete({
|
||||
where: {
|
||||
id: token.id
|
||||
id: foundRefreshToken.id
|
||||
}
|
||||
})
|
||||
reply.statusCode = 200
|
||||
return {}
|
||||
} catch {
|
||||
throw fastify.httpErrors.notFound()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -1,18 +1,16 @@
|
||||
import { randomUUID } from 'node:crypto'
|
||||
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import bcrypt from 'bcryptjs'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
import {
|
||||
bodyUserSchema,
|
||||
BodyUserSchemaType,
|
||||
userPublicSchema
|
||||
} from '../../../models/User.js'
|
||||
import type { BodyUserSchemaType } from '../../../models/User.js'
|
||||
import { bodyUserSchema, userPublicSchema } from '../../../models/User.js'
|
||||
import { sendEmail } from '../../../tools/email/sendEmail.js'
|
||||
import { API_URL } from '../../../tools/configurations/index.js'
|
||||
import { API_URL } from '../../../tools/configurations.js'
|
||||
|
||||
const queryPostSignupSchema = Type.Object({
|
||||
redirectURI: Type.Optional(Type.String({ format: 'uri-reference' }))
|
||||
|
@ -18,7 +18,7 @@ export const JWT_REFRESH_SECRET =
|
||||
export const JWT_ACCESS_EXPIRES_IN =
|
||||
process.env.JWT_ACCESS_EXPIRES_IN ?? '15 minutes'
|
||||
|
||||
export const SRC_URL = new URL('../../', import.meta.url)
|
||||
export const SRC_URL = new URL('../', import.meta.url)
|
||||
export const ROOT_URL = new URL('../', SRC_URL)
|
||||
export const EMAIL_URL = new URL('./email/', ROOT_URL)
|
||||
export const EMAIL_TEMPLATE_URL = new URL('./email-template.ejs', EMAIL_URL)
|
@ -1,38 +0,0 @@
|
||||
import dotenv from 'dotenv'
|
||||
import { readPackage } from 'read-pkg'
|
||||
import { FastifyDynamicSwaggerOptions } from 'fastify-swagger'
|
||||
|
||||
dotenv.config()
|
||||
|
||||
const packageJSON = await readPackage()
|
||||
|
||||
export const swaggerOptions: FastifyDynamicSwaggerOptions = {
|
||||
routePrefix: '/documentation',
|
||||
openapi: {
|
||||
info: {
|
||||
title: packageJSON.name,
|
||||
description: packageJSON.description,
|
||||
version: packageJSON.version
|
||||
},
|
||||
tags: [
|
||||
{ name: 'users' },
|
||||
{ name: 'guilds' },
|
||||
{ name: 'channels' },
|
||||
{ name: 'messages' },
|
||||
{ name: 'members' },
|
||||
{ name: 'uploads' }
|
||||
],
|
||||
components: {
|
||||
securitySchemes: {
|
||||
bearerAuth: {
|
||||
type: 'http',
|
||||
scheme: 'bearer',
|
||||
bearerFormat: 'JWT'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
exposeRoute: true,
|
||||
staticCSP: true,
|
||||
hideUntagged: true
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
import { Prisma } from '@prisma/client'
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import type { Prisma } from '@prisma/client'
|
||||
import type { Static } from '@sinclair/typebox'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
|
||||
export const queryPaginationSchema = {
|
||||
/** Maximum number of items to return */
|
||||
|
@ -1,6 +1,6 @@
|
||||
import dotenv from 'dotenv'
|
||||
import nodemailer from 'nodemailer'
|
||||
import type SMTPTransport from 'nodemailer/lib/smtp-transport.js'
|
||||
import type SMTPTransport from 'nodemailer/lib/smtp-transport/index.js'
|
||||
|
||||
dotenv.config()
|
||||
const EMAIL_PORT = parseInt(process.env.EMAIL_PORT ?? '465', 10)
|
||||
|
@ -3,11 +3,8 @@ import { URL, fileURLToPath } from 'node:url'
|
||||
|
||||
import ejs from 'ejs'
|
||||
|
||||
import { Language, Theme } from '../../models/UserSettings.js'
|
||||
import {
|
||||
EMAIL_LOCALES_URL,
|
||||
EMAIL_TEMPLATE_URL
|
||||
} from '../configurations/index.js'
|
||||
import type { Language, Theme } from '../../models/UserSettings.js'
|
||||
import { EMAIL_LOCALES_URL, EMAIL_TEMPLATE_URL } from '../configurations.js'
|
||||
import { emailTransporter, EMAIL_INFO } from './emailTransporter.js'
|
||||
|
||||
interface EmailTranslation {
|
||||
|
@ -8,7 +8,9 @@ await tap.test('tools/plugins/socket-io', async (t) => {
|
||||
const PORT = 3030
|
||||
const application = fastify()
|
||||
await application.register(fastifySocketIo)
|
||||
await application.listen(PORT)
|
||||
await application.listen({
|
||||
port: PORT
|
||||
})
|
||||
t.not(application.io, null)
|
||||
await application.close()
|
||||
})
|
||||
|
@ -3,8 +3,8 @@ import httpErrors from 'http-errors'
|
||||
import jwt from 'jsonwebtoken'
|
||||
|
||||
import prisma from '../database/prisma.js'
|
||||
import { UserJWT, UserRequest } from '../../models/User.js'
|
||||
import { JWT_ACCESS_SECRET } from '../configurations/index.js'
|
||||
import type { UserJWT, UserRequest } from '../../models/User.js'
|
||||
import { JWT_ACCESS_SECRET } from '../configurations.js'
|
||||
|
||||
const { Unauthorized, Forbidden, BadRequest } = httpErrors
|
||||
|
||||
@ -61,5 +61,5 @@ export default fastifyPlugin(
|
||||
request.user = user
|
||||
})
|
||||
},
|
||||
{ fastify: '3.x' }
|
||||
{ fastify: '4.x' }
|
||||
)
|
||||
|
@ -1,9 +1,10 @@
|
||||
import fastifyPlugin from 'fastify-plugin'
|
||||
import { Server as SocketIoServer, ServerOptions } from 'socket.io'
|
||||
import type { ServerOptions } from 'socket.io'
|
||||
import { Server as SocketIoServer } from 'socket.io'
|
||||
import { authorize } from '@thream/socketio-jwt'
|
||||
|
||||
import prisma from '../database/prisma.js'
|
||||
import { JWT_ACCESS_SECRET } from '../configurations/index.js'
|
||||
import { JWT_ACCESS_SECRET } from '../configurations.js'
|
||||
|
||||
interface EmitEventOptions {
|
||||
event: string
|
||||
@ -80,10 +81,10 @@ export default fastifyPlugin(
|
||||
emitToAuthorizedUsers,
|
||||
emitToMembers
|
||||
}
|
||||
await fastify.decorate('io', io)
|
||||
await fastify.addHook('onClose', (fastify) => {
|
||||
fastify.decorate('io', io)
|
||||
fastify.addHook('onClose', (fastify) => {
|
||||
fastify.io.instance.close()
|
||||
})
|
||||
},
|
||||
{ fastify: '3.x' }
|
||||
{ fastify: '4.x' }
|
||||
)
|
||||
|
@ -1,12 +1,12 @@
|
||||
import type { ResponseJWT } from './jwtToken.js'
|
||||
import {
|
||||
expiresIn,
|
||||
generateAccessToken,
|
||||
generateRefreshToken,
|
||||
ResponseJWT
|
||||
generateRefreshToken
|
||||
} from './jwtToken.js'
|
||||
import prisma from '../database/prisma.js'
|
||||
import { ProviderOAuth } from '../../models/OAuth.js'
|
||||
import { UserRequest } from '../../models/User.js'
|
||||
import type { ProviderOAuth } from '../../models/OAuth.js'
|
||||
import type { UserRequest } from '../../models/User.js'
|
||||
|
||||
interface ProviderData {
|
||||
name: string
|
||||
|
@ -1,14 +1,16 @@
|
||||
import { randomUUID } from 'node:crypto'
|
||||
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import jwt from 'jsonwebtoken'
|
||||
import ms from 'ms'
|
||||
|
||||
import prisma from '../database/prisma.js'
|
||||
import { UserJWT } from '../../models/User.js'
|
||||
import type { UserJWT } from '../../models/User.js'
|
||||
import {
|
||||
JWT_ACCESS_EXPIRES_IN,
|
||||
JWT_ACCESS_SECRET,
|
||||
JWT_REFRESH_SECRET
|
||||
} from '../configurations/index.js'
|
||||
} from '../configurations.js'
|
||||
|
||||
export interface ResponseJWT {
|
||||
accessToken: string
|
||||
@ -22,7 +24,7 @@ export const jwtSchema = {
|
||||
refreshToken: Type.String(),
|
||||
expiresIn: Type.Integer({
|
||||
description:
|
||||
'expiresIn is how long, in milliseconds, until the returned accessToken expires'
|
||||
'expiresIn is how long, in milliseconds, until the accessToken expires'
|
||||
}),
|
||||
type: Type.Literal('Bearer')
|
||||
}
|
||||
@ -30,15 +32,20 @@ export const jwtSchema = {
|
||||
export const expiresIn = ms(JWT_ACCESS_EXPIRES_IN)
|
||||
|
||||
export const generateAccessToken = (user: UserJWT): string => {
|
||||
return jwt.sign(user, JWT_ACCESS_SECRET, {
|
||||
expiresIn: JWT_ACCESS_EXPIRES_IN
|
||||
})
|
||||
return jwt.sign(user, JWT_ACCESS_SECRET, { expiresIn })
|
||||
}
|
||||
|
||||
export const generateRefreshToken = async (user: UserJWT): Promise<string> => {
|
||||
const refreshToken = jwt.sign(user, JWT_REFRESH_SECRET)
|
||||
const tokenUUID = randomUUID()
|
||||
const refreshToken = jwt.sign(
|
||||
{
|
||||
...user,
|
||||
tokenUUID
|
||||
},
|
||||
JWT_REFRESH_SECRET
|
||||
)
|
||||
await prisma.refreshToken.create({
|
||||
data: { token: refreshToken, userId: user.id }
|
||||
data: { token: tokenUUID, userId: user.id }
|
||||
})
|
||||
return refreshToken
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user