import { sign } from 'jsonwebtoken' ; import User from '../infra/typeorm/entities/User' ; import AppError from '@shared/errors/AppError' ; import IUsersRepository from '../repositories/IUsersRepository' ; import { inject , injectable } from 'tsyringe' ; import IHashProvider from '../providers/HashProvider/models/IHashProvider' ; import authConfig from '@config/auth' ; interface Request { email : string ; password : string ; } @ injectable ( ) class AuthenticateUserService { constructor ( @ inject ( 'UsersRepository' ) private usersRepository , @ inject ( 'HashProvider' ) private hashProvider ) { } public async execute ( { email , password , } ) { const user = await this . usersRepository . findByEmail ( email ) ; if ( ! user ) throw new AppError ( 'Incorrect email/password combination' , 401 ) ; const passwordMatched = await this . hashProvider . compareHash ( password , user . password ) ; if ( ! passwordMatched ) throw new AppError ( 'Incorrect email/password combination' , 401 ) ; const token = sign ( { } , authConfig . jwt . secret , { subject : user . id , expiresIn : authConfig . jwt . expires , } ) ; return { user , token } ; } } export default AuthenticateUserService ;