Skip to content

Commit

Permalink
valid old password when update password
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed-Mustafa132 committed Oct 14, 2024
1 parent 11f4247 commit 85120f2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
26 changes: 21 additions & 5 deletions controllers/handelUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ const getByid = async (req, res) => {
const updateOne = async (req, res) => {
try {
const { id } = req.params;
const { password, ...newUpdate } = req.body; // Extract other fields from req.body
const { newPassword, ...newUpdate } = req.body; // Extract other fields from req.body
const updateData = { ...newUpdate };

// Handle password hashing if provided
if (password) {
const hashedPassword = await bcrypt.hash(password, 10);
if (newPassword) {
const hashedPassword = await bcrypt.hash(newPassword, 10);
updateData.password = hashedPassword;
}

Expand All @@ -65,6 +65,7 @@ const updateOne = async (req, res) => {
};



const createone = async (req, res) => {
try {
// Extract user data from the request body
Expand Down Expand Up @@ -302,5 +303,20 @@ const resetPassword = async (req, res) => {
res.status(500).json({ message: "Server error, please try again later" });
}
};
const verifyPassword = async (req, res) => {
const { password } = req.body;
console.log("password is",password)
try {
const user = await usermodel.findById(req.params.id);
if (!user) return res.status(404).send('User not found');

const isValid = await bcrypt.compare(password, user.password);
if (!isValid) return res.status(401).send('passord is not valid');

res.send( isValid);
} catch (err) {
res.status(500).send(err.message);
}
}

module.exports = { getall, getByid, updateOne,createone,deleteOne,deleteall,login, uploadImage,getAllProfessors, forgotPassword, resetPassword }
module.exports = { getall, getByid, updateOne,createone,deleteOne,deleteall,login, uploadImage,getAllProfessors, forgotPassword, resetPassword,verifyPassword }
4 changes: 3 additions & 1 deletion routes/userRoute.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const upload = require('../Middlewares/uploadConfig');

const { getall, getByid, updateOne,createone,deleteOne,deleteall,login,uploadImage, getAllProfessors, forgotPassword, resetPassword} = module.require("../controllers/handelUser");
const { getall, getByid, updateOne,createone,deleteOne,deleteall,login,uploadImage, getAllProfessors, forgotPassword, resetPassword,verifyPassword} = module.require("../controllers/handelUser");

const express = module.require('express')
const router=express.Router();
Expand All @@ -18,4 +18,6 @@ router.post('/login',login)
router.post('/uploadUserImage/:id',upload.single('image'),uploadImage);
router.post('/forgot-password', forgotPassword);
router.post('/reset-password/:token', resetPassword);
router.post('/verify/:id',verifyPassword);

module.exports=router;

0 comments on commit 85120f2

Please sign in to comment.