diff --git a/api/v1/modules/user/middlewares/UserMiddleware.ts b/api/v1/modules/user/middlewares/UserMiddleware.ts index b1296d80..5eacd3cd 100644 --- a/api/v1/modules/user/middlewares/UserMiddleware.ts +++ b/api/v1/modules/user/middlewares/UserMiddleware.ts @@ -185,11 +185,11 @@ export const checkRealUser: any = async (req: Request, res: Response, next: Next const data = req.body // Comprobar si el usuario existe const userByEmail = await User.findOne({ email: data.usernameOrEmail }) - const userByUsername=await User.findOne({ username: data.usernameOrEmail }) + const userByUsername = await User.findOne({ username: data.usernameOrEmail }) if (!userByEmail && !userByUsername) { const message = 'User not found' ApiResponse.sendError(res, [{ title: 'Not Found', detail: message }], 404) - }else { + } else { next() } } catch (error: any) { @@ -211,19 +211,18 @@ export const checkCorrectToken: any = async (req: Request, res: Response, next: return } const decodedToken = verifyJWT(token) - if(!decodedToken.sub){ + if (!decodedToken.sub) { const message = 'Incorrect token' ApiResponse.sendError(res, [{ title: 'Bad Request', detail: message }], 401) return } - const user= await User.findById(decodedToken.sub) - if(!user){ + const user = await User.findById(decodedToken.sub) + if (!user) { const message = 'User not found' ApiResponse.sendError(res, [{ title: 'Not Found', detail: message }], 404) - }else { + } else { next() } - } catch (error: any) { ApiResponse.sendError(res, [ { @@ -234,21 +233,24 @@ export const checkCorrectToken: any = async (req: Request, res: Response, next: } } -export const checkRepeatedPassword: any = async (req: Request, res: Response, next: NextFunction) => { +export const checkRepeatedPassword: any = async ( + req: Request, + res: Response, + next: NextFunction +) => { try { - const {newPassword,repeatedPassword} = req.body - const compare=newPassword === repeatedPassword - if(newPassword.lenght ===0 || repeatedPassword.lenght===0){ + const { newPassword, repeatedPassword } = req.body + const compare = newPassword === repeatedPassword + if (newPassword.lenght === 0 || repeatedPassword.lenght === 0) { const message = 'There are empty fields' ApiResponse.sendError(res, [{ title: 'Bad Request', detail: message }], 400) - }else if(!(compare)){ + } else if (!compare) { const message = 'The passwords dont match' ApiResponse.sendError(res, [{ title: 'Bad Request', detail: message }], 400) - }else { - req.body.encryptedPassword=await encrypt(newPassword) + } else { + req.body.encryptedPassword = await encrypt(newPassword) next() } - } catch (error: any) { ApiResponse.sendError(res, [ { @@ -273,7 +275,10 @@ export const checkLoginUser: any = async (req: Request, res: Response, next: Nex return } // Comprobar si el usuario existe - const user = await User.findOne({ username: data.username }) + const userByEmail = await User.findOne({ email: data.username }) + const userByUsername = await User.findOne({ username: data.username }) + const user = userByEmail ?? userByUsername + if (!user) { const message = 'User not found' ApiResponse.sendError(res, [{ title: 'Not Found', detail: message }], 404) diff --git a/api/v1/modules/user/services/UserService.ts b/api/v1/modules/user/services/UserService.ts index 33aa7147..16b21522 100644 --- a/api/v1/modules/user/services/UserService.ts +++ b/api/v1/modules/user/services/UserService.ts @@ -100,45 +100,44 @@ export const deleteUser: any = async (id: any, role: string) => { } } -export const sendEmail: any = async (to: string, subject: string, text: string,html: string) =>{ - try{ - const from= process.env.SENGRID_EMAIL ?? '' +export const sendEmail: any = async (to: string, subject: string, text: string, html: string) => { + try { + const from = process.env.SENGRID_EMAIL ?? '' const msg = { to, from, subject, text, html, - }; + } await sgMail - .send(msg) - .then(() => { - console.log('Email sent') - }) - .catch((error:any) => { - console.error(error) - }) - }catch(error:any){ + .send(msg) + .then(() => { + console.log('Email sent') + }) + .catch((error: any) => { + console.error(error) + }) + } catch (error: any) { console.error('Error creating your request:', error) throw error } } -export const createChangePasswordRequest: any = async (data: any,originalUrl: string) => { +export const createChangePasswordRequest: any = async (data: any, originalUrl: string) => { try { const userByEmail = await User.findOne({ email: data.usernameOrEmail }) - const userByUsername=await User.findOne({ username: data.usernameOrEmail }) + const userByUsername = await User.findOne({ username: data.usernameOrEmail }) const user = userByEmail ?? userByUsername const id = user?._id.toString() const token = generateJWTWithSoonerExpiration(id) - const result = originalUrl+"/"+token - const text=`To change the forgotten password, access this link: ${result}. \n + const result = originalUrl + '/' + token + const text = `The user with the username: ${user?.username} and email: ${user?.email} has requested to change the password. + To change the forgotten password, access this link: ${result}. \n \n In case of error, simply ignore the message. Thank you very much for using IT TALENT :3` - await sendEmail(user?.email,'Verify password change', - text,` ${text} ` - ) + await sendEmail(user?.email, 'Verify password change', text, ` ${text} `) } catch (error) { console.error('Error creating your request:', error) throw error @@ -147,7 +146,10 @@ export const createChangePasswordRequest: any = async (data: any,originalUrl: st export const loginUser: any = async (data: any) => { try { - const user = await User.findOne({ username: data.username }) + const userByEmail = await User.findOne({ email: data.username }) + const userByUsername = await User.findOne({ username: data.username }) + const user = userByEmail ?? userByUsername + const id = user?._id.toString() const token = generateJWT(id) const result = { token, user } @@ -166,5 +168,6 @@ export default { updateUserProfilePicture, updateUserPassword, deleteUser, - loginUser,createChangePasswordRequest + loginUser, + createChangePasswordRequest, } diff --git a/app/src/pages/auth/Login.jsx b/app/src/pages/auth/Login.jsx index 2012342f..cd8c54c0 100644 --- a/app/src/pages/auth/Login.jsx +++ b/app/src/pages/auth/Login.jsx @@ -106,13 +106,15 @@ export default function Login() { }}>