Postingan lainnya
Kelas Premium!
Belajar Javascript untuk website
Gunakan kupon "mulaijs" untuk diskon 75 ribu!
Error : data and salt argument required
Hari ini saya sedang belajar nodejs , nah saya coba buat sistem login, lalu saya menemukan error seperti ini : Error: data and salt arguments required, kira-kira solving nya gimana ya? terimakasih
exports.login = async (req, res) => {
try {
// return res.status(400).render('login-form', {
// errorMessage: 'please provide your email',
// });
let email = req.body.email;
let password = req.body.password;
let HashedPassword = await bcrypt.hash(password, 10);
console.log(HashedPassword);
database.query('SELECT * FROM users WHERE email = ? ', [email], async (error, results) => {
let match = await bcrypt.compare(password, HashedPassword);
if (!results || !match) {
res.status(401).render('login-form', {
errorMessage: 'Email or password is incorrect',
});
} else {
// let sqlId = 'SELECT id FROM users WHERE id = email';
const id = results[0].id;
const token = jwt.sign({ id }, process.env.JWT_SECRET, {
expiresIn: process.env.JWT_EXPIRES_IN,
});
console.log('The token is : ' + token);
const cookieOptions = {
expires: new Date(Date.now() + process.env.JWT_COOKIE_EXPIRES * 24 * 60 * 60 * 1000),
httpOnly: true,
};
res.cookie('jwt', token, cookieOptions);
res.status(200).send('<h1>Berhasil Masuk</h1>');
}
});
} catch (error) {
console.log('Somethings wrong ' + error);
}
};
1 Jawaban:
<div>Saya coba bantu jawab ya, coba ubah bagian ini :</div><pre>let HashedPassword = await bcrypt.hash(password, 10); console.log(HashedPassword);<br></pre><div><br>menjadi :</div><pre>let saltRounds = await bcrypt.genSalt(10); let HashedPassword = await bcrypt.hash(password, saltRounds); console.log(HashedPassword);</pre><div><br>Silahkan cek juga dokumentasi resminya disini <a href="https://www.npmjs.com/package/bcrypt">https://www.npmjs.com/package/bcrypt</a><br>Semoga berhasil.</div><div> </div>