feat: migrate from express to fastify

This commit is contained in:
Divlo
2021-10-24 04:18:18 +02:00
parent 714cc643ba
commit b77e602358
281 changed files with 19768 additions and 22895 deletions

View File

@ -1,19 +1,45 @@
import { Request, Response, Router } from 'express'
import { Type } from '@sinclair/typebox'
import { FastifyPluginAsync, FastifySchema } from 'fastify'
import { authenticateUser } from '../../../tools/middlewares/authenticateUser'
import { ForbiddenError } from '../../../tools/errors/ForbiddenError'
import { deleteEveryRefreshTokens } from '../__utils__/deleteEveryRefreshTokens'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import authenticateUser from '../../../tools/plugins/authenticateUser.js'
export const signoutEveryDevicesRouter = Router()
signoutEveryDevicesRouter.delete(
'/users/signout',
authenticateUser,
async (req: Request, res: Response) => {
if (req.user == null) {
throw new ForbiddenError()
const deleteSignoutSchema: FastifySchema = {
description: 'Signout the user to every connected devices',
tags: ['users'] as string[],
security: [
{
bearerAuth: []
}
await deleteEveryRefreshTokens(req.user.current.id)
res.status(200).json({})
] as Array<{ [key: string]: [] }>,
response: {
200: Type.Object({}),
400: fastifyErrors[400],
401: fastifyErrors[401],
403: fastifyErrors[403],
500: fastifyErrors[500]
}
)
} as const
export const deleteSignoutUser: FastifyPluginAsync = async (fastify) => {
await fastify.register(authenticateUser)
fastify.route({
method: 'DELETE',
url: '/users/signout',
schema: deleteSignoutSchema,
handler: async (request, reply) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
await prisma.refreshToken.deleteMany({
where: {
userId: request.user.current.id
}
})
reply.statusCode = 200
return {}
}
})
}