I create an account and then right away try to login with the same credentials but I continue to get the error, “Invalid Password”.
router.post('/signup', async (req, res) => {
let { username, password } = req.body;
console.log(password)
try {
const existingUser = await User.findOne({ username: username });
if (existingUser) {
return res.status(400).json({ message: 'Username already exists' });
}
const hashedPassword = await bcryptjs.hash(password.trim(), 10);
const user = new User({
username: username,
password: hashedPassword,
following: [],
followers: [],
});
const newUser = await user.save();
res.status(201).json({user: newUser });
} catch (err) {
res.status(500).json({ message: err.message });
}
});
router.post('/login', async (req, res) => {
let { username, password } = req.body;
try {
const user = await User.findOne({ username: username});
if (!user) {
return res.status(400).json({ message: 'User not found' });
}
console.log(user)
const passwordMatches = await bcryptjs.compare(password.trim(), user.password.trim());
if (!passwordMatches) {
return res.status(400).json({ message: 'Invalid password' });
}
res.json({ user: user });
} catch (err) {
res.status(500).json({ message: err.message });
}
});
It should return to be a true comparison when I pass the right credentials.
New contributor
Emilio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.