import { Request , Response , NextFunction } from 'express' ; import { getRepository } from 'typeorm' ; import { Role } from 'typeorm/entities/users/types' ; import { User } from 'typeorm/entities/users/User' ; import { JwtPayload } from 'types/JwtPayload' ; import { createJwtToken } from 'utils/createJwtToken' ; import { CustomError } from 'utils/response/custom-error/CustomError' ; export const login = async ( req , res , next ) => { const { email , password } = req . body ; const userRepository = getRepository ( User ) ; try { const user = await userRepository . findOne ( { where : { email } } ) ; if ( ! user ) { const customError = new CustomError ( 404 , 'General' , 'Not Found' , [ 'Incorrect email or password' ] ) ; return next ( customError ) ; } if ( ! user . checkIfPasswordMatch ( password ) ) { const customError = new CustomError ( 404 , 'General' , 'Not Found' , [ 'Incorrect email or password' ] ) ; return next ( customError ) ; } const jwtPayload = { id : user . id , name : user . name , email : user . email , role : user . role as Role , created_at : user . created_at , } ; try { const token = createJwtToken ( jwtPayload ) ; res . customSuccess ( 200 , 'Token successfully created.' , `Bearer ${ token }` ) ; } catch ( err ) { const customError = new CustomError ( 400 , 'Raw' , "Token can't be created" , null , err ) ; return next ( customError ) ; } } catch ( err ) { const customError = new CustomError ( 400 , 'Raw' , 'Error' , null , err ) ; return next ( customError ) ; } } ;