Skip to content

Commit 4641ea4

Browse files
committed
feat: Implementar controller de reset de senha
1 parent d9f827c commit 4641ea4

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { FastifyReply, FastifyRequest } from 'fastify'
2+
import { z } from 'zod'
3+
4+
import { InvalidCredentialsError } from '@/services/errors/invalid-credentials-error'
5+
import { makeResetPasswordService } from '@/services/factories/users/make-reset-password-service'
6+
7+
export async function resetPassword(
8+
request: FastifyRequest,
9+
reply: FastifyReply,
10+
) {
11+
const resetPasswordBodySchema = z.object({
12+
code: z.number().int().min(100000).max(999999),
13+
password: z.string().min(6),
14+
userId: z.string().cuid(),
15+
})
16+
17+
const { code, password, userId } = resetPasswordBodySchema.parse(request.body)
18+
19+
try {
20+
const resetPasswordService = makeResetPasswordService()
21+
22+
await resetPasswordService.execute({ code, password, userId })
23+
} catch (err) {
24+
if (err instanceof InvalidCredentialsError) {
25+
return reply.status(404).send({ message: err.message })
26+
}
27+
28+
throw err
29+
}
30+
31+
return reply.status(204).send()
32+
}

0 commit comments

Comments
 (0)