implement self/update_password endpoint

This commit is contained in:
srgooglo 2022-10-25 14:41:37 +00:00
parent 2474fe0a12
commit 80bf198908
2 changed files with 14 additions and 5 deletions

View File

@ -1,6 +1,7 @@
import { Controller } from "linebridge/dist/server"
import passport from "passport"
import lodash from "lodash"
import bcrypt from "bcrypt"
import SessionController from "../SessionController"
@ -434,17 +435,17 @@ export default class UserController extends Controller {
return res.status(404).json({ message: "User not found" })
}
const currentPasswordHash = await bcrypt.hash(req.selection.currentPassword, parseInt(process.env.BCRYPT_ROUNDS ?? 3))
const isPasswordValid = await bcrypt.compareSync(currentPasswordHash, user.password)
const isPasswordValid = await bcrypt.compareSync(req.selection.currentPassword, user.password)
if (!isPasswordValid) {
return res.status(401).json({ message: "Invalid password" })
return res.status(401).json({
message: "Current password dont match"
})
}
const result = await updatePassword({
user_id: req.user._id,
newPassword: req.selection.newPassword,
password: req.selection.newPassword,
}).catch((error) => {
res.status(500).json({ message: error.message })
return null

View File

@ -4,6 +4,14 @@ import { User } from "../../../models"
export default async function (payload) {
const { user_id, password } = payload
if (!user_id) {
throw new Error("Missing user_id")
}
if (!password) {
throw new Error("Missing password")
}
const user = await User.findById(user_id)
if (!user) {