/var/www/greso.tech/server/nsm/controllers/auth
Edit: /var/www/greso.tech/server/nsm/controllers/auth/changePassword.js (2088B)
module.exports = function ({ db, logger }) {
const { NotFound, BadRequest } = require('../../responseModels/errors')
const { isSet, isEmail, isPhone, isEmpty, minLength, isNumber } = require('../../utils/validator.util')
const uuid = require('uuid');
const bcrypt = require('bcryptjs')
return async (req, res, next) => {
logger.child({ path: req.path }).info()
let t1 = performance.now()
try {
const { linkId, pass1, pass2 } = req.body
if (!isSet(linkId) || isEmpty(linkId)) {
throw new BadRequest({ code: 404, lang: req.user?.languageCode })
}
if (!isSet(pass1) || isEmpty(pass1)) {
throw new BadRequest({ code: 1005, lang: req.user?.languageCode })
}
if (!isSet(pass2) || isEmpty(pass2)) {
throw new BadRequest({ code: 1005, lang: req.user?.languageCode })
}
if (pass1 != pass2) {
throw new BadRequest({ code: 1006, lang: req.user?.languageCode })
}
let foundLink = null
let foundEmail = null
foundLink = (
await db.links.findOne({ where: { id: linkId } })
)
if (!foundLink) {
throw new BadRequest({ code: 404, lang: req.user?.languageCode })
}
if(foundLink.type != 'restore_pass'){
throw new BadRequest({ code: 404, lang: req.user?.languageCode })
}
foundEmail = (
await db.userEmail.findOne({ where: { userId: foundLink.userId } })
)
if (!foundEmail) {
throw new BadRequest({ code: 404, lang: req.user?.languageCode })
}
const salt = await bcrypt.genSalt(10)
const passwordHash = await bcrypt.hash(pass1, salt)
foundEmail.passwordHash = passwordHash
await foundEmail.save()
await foundLink.destroy()
const time = performance.now() - t1
return res.status(200).json({ status: 'success', time })
// const result = await SERVICES.MailService.SendResetPassword(email, link.id)
// if(result.accepted.length>0){
// const time = performance.now() - t1
// return res.status(200).json({ status: 'success', time })
// }
// throw new BadRequest({ code: 404, lang: req.user?.languageCode })
} catch (err) {
next(err)
}
}
}