OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

bcrypt hashing password nodejs

  • Thread starter Thread starter anas
  • Start date Start date
A

anas

Guest
i keep getting this error "Invalid password" even if the password and email are correct which makes no sense pls help me to solve this problem i think that i am comparing a hashed password and a plain text password but i dont know how to solve it

Code:
app.post('/login0', (req, res) => {
    const { email, password } = req.body;
    const sql = "SELECT * FROM user WHERE email = ?";
    const values = [email];

    db.query(sql, values, (err, result) => {
        if (err) {
            console.error("Database error: ", err);
            return res.status(500).json({ message: "Login failed: Database error" });
        }
        
        if (result.length > 0) {
            const storedPassword = result[0].password;
            if (bcrypt.compareSync(password, storedPassword)) {
                console.log("Password match, login successful");
                return res.status(200).json({ message: "Login successful", user: result[0] });
            } else {
                console.log("Invalid password");
                return res.status(401).json({ message: "Invalid password" });
            }
        } else {
            console.log("Account not found");
            return res.status(401).json({ message: "Account not found" });
        }
    });
});

the passwords are stored hashed in the data base

Code:
app.post('/add_user', (req, res) => {
console.log("Post request on /add_user");

const { name, lastname, email, password, address, number } = req.body;

bcrypt.genSalt(10, (err, salt) => {
    if (err) {
        console.error('Error generating salt:', err);
        res.status(500).send('Server error');
        return;
    }

    bcrypt.hash(password, salt, (err, hashedPassword) => {
        if (err) {
            console.error('Error hashing password:', err);
            res.status(500).send('Server error');
            return;
        }

        const sql = "INSERT INTO user (name, lastname, email, password, address, number) VALUES (?, ?, ?, ?, ?, ?)";
        const values = [name, lastname, email, hashedPassword, address, number];

        db.query(sql, values, (err, result) => {
            if (err) {
                console.error('Error inserting into database:', err);
                res.status(500).send('Server error');
                return;
            }
            console.log("User added successfully");
            res.status(200).send('User added successfully');
        });
    });
});

});

<p>i keep getting this error "Invalid password" even if the password and email are correct which makes no sense pls help me to solve this problem i think that i am comparing a hashed password and a plain text password but i dont know how to solve it</p>
<pre><code>app.post('/login0', (req, res) => {
const { email, password } = req.body;
const sql = "SELECT * FROM user WHERE email = ?";
const values = ;

db.query(sql, values, (err, result) => {
if (err) {
console.error("Database error: ", err);
return res.status(500).json({ message: "Login failed: Database error" });
}

if (result.length > 0) {
const storedPassword = result[0].password;
if (bcrypt.compareSync(password, storedPassword)) {
console.log("Password match, login successful");
return res.status(200).json({ message: "Login successful", user: result[0] });
} else {
console.log("Invalid password");
return res.status(401).json({ message: "Invalid password" });
}
} else {
console.log("Account not found");
return res.status(401).json({ message: "Account not found" });
}
});
});

</code></pre>
<p>the passwords are stored hashed in the data base</p>
<pre><code>app.post('/add_user', (req, res) => {
console.log("Post request on /add_user");

const { name, lastname, email, password, address, number } = req.body;

bcrypt.genSalt(10, (err, salt) => {
if (err) {
console.error('Error generating salt:', err);
res.status(500).send('Server error');
return;
}

bcrypt.hash(password, salt, (err, hashedPassword) => {
if (err) {
console.error('Error hashing password:', err);
res.status(500).send('Server error');
return;
}

const sql = "INSERT INTO user (name, lastname, email, password, address, number) VALUES (?, ?, ?, ?, ?, ?)";
const values = [name, lastname, email, hashedPassword, address, number];

db.query(sql, values, (err, result) => {
if (err) {
console.error('Error inserting into database:', err);
res.status(500).send('Server error');
return;
}
console.log("User added successfully");
res.status(200).send('User added successfully');
});
});
});
</code></pre>
<p>});</p>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top