bycryptjs compare password return false every time
i write a code for signup and login . i encrypt password and store in data base and after that in during signin when i compare the password is return false even the password is true and encypt and decrypt correct i try debugging statements also but never find the solution here is the code
const loginController = async (req, res) => { try { const { name, password } = req.body; console.log("Received password:", password); if (!name || !password) { console.log("Name or password not provided"); return res.status(400).json({ message: "Please provide name and password" }); }
const user = await UserModel.findOne({ name });
if (!user) {
console.log("User not found with name:", name);
return res.status(404).json({ message: "User not found" });
}
// Check if password is correct
console.log("User hashed password:", user.password);
const isMatch = await bcrypt.compare(password.trim(), user.password);
console.log("Password match result:", isMatch);
if (isMatch) {
console.log("Login successful for user:", user.name);
return res.status(200).json({ // Corrected status code from 201 to 200 for successful login
message: "Login successful",
user: {
_id: user._id,
name: user.name,
token: generateToken(user),
},
});
} else {
console.log("Invalid password for user:", user.name);
return res.status(401).json({ message: "Invalid password" });
}
} catch (error) { console.error("Signin error:", error); return res.status(500).json({ message: "Server error" }); } };
const signupController = async (req, res) => { try { const { name, email, password } = req.body;
// Check if all fields are provided
if (!name || !email || !password) {
return res
.status(400)
.json({ message: "Please provide name, email, and password" });
}
// Check if user already exists
const isUserExist = await UserModel.findOne({ email });
if (isUserExist) {
return res.status(409).json({ message: "Email is already registered" });
}
const isUserNameExist = await UserModel.findOne({ name });
if (isUserNameExist) {
return res.status(409).json({ message: "Name is already taken" });
}
// Hash password
const hashedPassword = await bcrypt.hash(password, 10);
console.log("Hashed password while signup:", hashedPassword);
// Create new user
const user = await UserModel.create({
name,
email,
password: hashedPassword,
});
console.log("password stored in database: "+password)
res.status(201).json({
message: "User created successfully",
user: {
_id: user._id,
name: user.name,
email: user.email,
token: generateToken(user),
},
});
} catch (error) { if (error.name === "ValidationError") { return res .status(422) .json({ message: "Validation error", details: error.message }); } console.error("Signup error:", error); res.status(500).json({ message: "Server error" }); } };
the console log statement are following " Hashed password while signup: $2a$10$BQzL/xkcX9q73ewDLv5bqeo9w0H5fBi/4oeT6imaYmFjZymbEGoI6 $2a$10$xSrww0yg/L9oiQTNorGVLew54TikR3N507loj9S8RzY0lwlrd3Xfu password stored in database: abc Received password: abc User hashed password: $2a$10$xSrww0yg/L9oiQTNorGVLew54TikR3N507loj9S8RzY0lwlrd3Xfu Password match result: false Invalid password for user: abc"
please tell if any problem there
I am facing the same problem. Is there any update about this?
Related to an open issue: https://github.com/kelektiv/node.bcrypt.js/issues/1037