feat: migrate from express to fastify
This commit is contained in:
@ -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 {}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user