feat: migrate from express to fastify
This commit is contained in:
39
src/services/users/confirm-email/__test__/get.test.ts
Normal file
39
src/services/users/confirm-email/__test__/get.test.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import { application } from '../../../../application.js'
|
||||
import { userExample } from '../../../../models/User.js'
|
||||
import { prismaMock } from '../../../../__test__/setup.js'
|
||||
|
||||
describe('GET /users/confirm-email', () => {
|
||||
it('should succeeds', async () => {
|
||||
prismaMock.user.findFirst.mockResolvedValue(userExample)
|
||||
prismaMock.user.update.mockResolvedValue({
|
||||
...userExample,
|
||||
isConfirmed: true,
|
||||
temporaryToken: null
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: '/users/confirm-email',
|
||||
query: {
|
||||
temporaryToken: userExample.temporaryToken ?? ''
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(200)
|
||||
})
|
||||
|
||||
it('should fails with invalid `temporaryToken`', async () => {
|
||||
prismaMock.user.findFirst.mockResolvedValue(null)
|
||||
prismaMock.user.update.mockResolvedValue({
|
||||
...userExample,
|
||||
isConfirmed: true,
|
||||
temporaryToken: null
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: '/users/confirm-email',
|
||||
query: {
|
||||
temporaryToken: userExample.temporaryToken ?? ''
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(403)
|
||||
})
|
||||
})
|
59
src/services/users/confirm-email/get.ts
Normal file
59
src/services/users/confirm-email/get.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import prisma from '../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../models/utils.js'
|
||||
import { userSchema } from '../../../models/User.js'
|
||||
|
||||
const queryGetConfirmEmailSchema = Type.Object({
|
||||
redirectURI: Type.Optional(Type.String({ format: 'uri-reference' })),
|
||||
temporaryToken: userSchema.temporaryToken
|
||||
})
|
||||
|
||||
type QueryGetConfirmEmailSchemaType = Static<typeof queryGetConfirmEmailSchema>
|
||||
|
||||
const getConfirmEmailSchema: FastifySchema = {
|
||||
description: 'Confirm the account of the user.',
|
||||
tags: ['users'] as string[],
|
||||
querystring: queryGetConfirmEmailSchema,
|
||||
response: {
|
||||
200: Type.String(),
|
||||
400: fastifyErrors[400],
|
||||
403: fastifyErrors[403],
|
||||
500: fastifyErrors[500]
|
||||
}
|
||||
} as const
|
||||
|
||||
export const getConfirmEmail: FastifyPluginAsync = async (fastify) => {
|
||||
fastify.route<{
|
||||
Querystring: QueryGetConfirmEmailSchemaType
|
||||
}>({
|
||||
method: 'GET',
|
||||
url: '/users/confirm-email',
|
||||
schema: getConfirmEmailSchema,
|
||||
handler: async (request, reply) => {
|
||||
const { redirectURI, temporaryToken } = request.query
|
||||
const user = await prisma.user.findFirst({
|
||||
where: {
|
||||
temporaryToken,
|
||||
isConfirmed: false
|
||||
}
|
||||
})
|
||||
if (user == null) {
|
||||
throw fastify.httpErrors.forbidden()
|
||||
}
|
||||
await prisma.user.update({
|
||||
where: { id: user.id },
|
||||
data: {
|
||||
temporaryToken: null,
|
||||
isConfirmed: true
|
||||
}
|
||||
})
|
||||
if (redirectURI == null) {
|
||||
reply.statusCode = 200
|
||||
return 'Success, your email has been confirmed, you can now signin!'
|
||||
}
|
||||
await reply.redirect(redirectURI)
|
||||
}
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user