Skip to content

Commit

Permalink
Merge pull request #25 from MatheusSanchez/fix-unauthorized-response
Browse files Browse the repository at this point in the history
Fixing unauthorized response
  • Loading branch information
pedrodecf authored Jan 29, 2024
2 parents ed8ddca + 510c063 commit e6649eb
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/controller/session/authUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,38 @@ import { FastifyReply, FastifyRequest } from 'fastify'
import { AuthUserUseCase } from '../../use-cases/authUserUseCase'
import { z } from 'zod'
import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository'
import { InvalidCredentialsError } from '../../use-cases/errors/InvalidCredentialsError'

export async function authUser(
export async function authUser(
request: FastifyRequest,
response: FastifyReply,
) {

const userRepository = new PrismaUsersRepository()
const authUserUseCase = new AuthUserUseCase(userRepository)

const AuthUserUseCaseSchema = z.object({
email: z.string().email(),
password: z.string()
password: z.string(),
})

const { email, password } = AuthUserUseCaseSchema.parse(request.body)
const { email, password } = AuthUserUseCaseSchema.parse(request.body)

const { user } = await authUserUseCase.execute({ email, password })
try {
const { user } = await authUserUseCase.execute({ email, password })

const token = await response.jwtSign(
{},
{
sign: {
sub: user.id
const token = await response.jwtSign(
{},
{
sign: {
sub: user.id,
},
},
},
)


return response.status(200).send({ user, token })
}
)

return response.status(200).send({ user, token })
} catch (e) {
if (e instanceof InvalidCredentialsError) {
return response.status(401).send()
}
}
}

0 comments on commit e6649eb

Please sign in to comment.