feat: migrate from express to fastify
This commit is contained in:
@ -1,36 +1,59 @@
|
||||
import { Request, Response, Router } from 'express'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import { authenticateUser } from '../../../tools/middlewares/authenticateUser'
|
||||
import OAuth, { AuthenticationStrategy } from '../../../models/OAuth'
|
||||
import UserSetting from '../../../models/UserSetting'
|
||||
import { ForbiddenError } from '../../../tools/errors/ForbiddenError'
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
import authenticateUser from '../../../tools/plugins/authenticateUser.js'
|
||||
import { userCurrentSchema } from '../../../models/User.js'
|
||||
|
||||
export const getCurrentRouter = Router()
|
||||
|
||||
getCurrentRouter.get(
|
||||
'/users/current',
|
||||
authenticateUser,
|
||||
async (req: Request, res: Response) => {
|
||||
if (req.user == null) {
|
||||
throw new ForbiddenError()
|
||||
const getCurrentUserSchema: FastifySchema = {
|
||||
description: 'GET the current connected user',
|
||||
tags: ['users'] as string[],
|
||||
security: [
|
||||
{
|
||||
bearerAuth: []
|
||||
}
|
||||
const settings = await UserSetting.findOne({
|
||||
where: { userId: req.user.current.id }
|
||||
})
|
||||
const OAuths = await OAuth.findAll({
|
||||
where: { userId: req.user.current.id }
|
||||
})
|
||||
const strategies: AuthenticationStrategy[] = OAuths.map((oauth) => {
|
||||
return oauth.provider
|
||||
})
|
||||
if (req.user.current.password != null) {
|
||||
strategies.push('local')
|
||||
}
|
||||
return res.status(200).json({
|
||||
user: req.user.current,
|
||||
settings,
|
||||
currentStrategy: req.user.currentStrategy,
|
||||
strategies
|
||||
})
|
||||
] as Array<{ [key: string]: [] }>,
|
||||
response: {
|
||||
200: userCurrentSchema,
|
||||
400: fastifyErrors[400],
|
||||
401: fastifyErrors[401],
|
||||
403: fastifyErrors[403],
|
||||
500: fastifyErrors[500]
|
||||
}
|
||||
)
|
||||
} as const
|
||||
|
||||
export const getCurrentUser: FastifyPluginAsync = async (fastify) => {
|
||||
await fastify.register(authenticateUser)
|
||||
|
||||
fastify.route({
|
||||
method: 'GET',
|
||||
url: '/users/current',
|
||||
schema: getCurrentUserSchema,
|
||||
handler: async (request, reply) => {
|
||||
if (request.user == null) {
|
||||
throw fastify.httpErrors.forbidden()
|
||||
}
|
||||
const settings = await prisma.userSetting.findFirst({
|
||||
where: { userId: request.user.current.id }
|
||||
})
|
||||
const OAuths = await prisma.oAuth.findMany({
|
||||
where: { userId: request.user.current.id }
|
||||
})
|
||||
const strategies = OAuths.map((oauth) => {
|
||||
return oauth.provider
|
||||
})
|
||||
if (request.user.current.password != null) {
|
||||
strategies.push('local')
|
||||
}
|
||||
reply.statusCode = 200
|
||||
return {
|
||||
user: {
|
||||
...request.user.current,
|
||||
settings,
|
||||
currentStrategy: request.user.currentStrategy,
|
||||
strategies
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user