I’ve had this working before, but now it’s broken for some reason. I’m using MariaDB
as a database, and need to compare passwords, but it’s giving me an error Unexpected Identifier on "bcrypt" after await
. When I remove the await
, it works, but even if the password is wrong. What am I doing wrong here? Thanks
Edit: I forgot to include the user info after .then. I must of deleted it with some comments when I posted this question. I added it back. Here is my code:
//data connection pool const pool = require('../models/db'); const { handleError, ErrorHandler } = require('../helpers/error'); const bcrypt = require('bcrypt'); module.exports = (req, res) => { //destructuring assignment👇 const {user_name, password} = req.body; let hashedPassword; //TODO: hash password var sql = `SELECT * FROM Govt_profiles WHERE (user_name = ?)`; //pool.query() is shortcut for pool.getConnection() + connection.query() + connection.release() pool.query(sql, [user_name], async (err, data) => { }) .then(rows => { const user = rows.find( user => user['User_Name'] === user_name); if (user == null) { return res.status(400).send('Cannot find user'); } try { if (await bcrypt.compare(password, user['Password'])) { console.log('Password: ', password, 'hashedPassword: ', hashedPassword); console.log("this is a loginUser: "); console.log(req.session); //return res.redirect('questions') return res.render('user-progress', { userName: req.session.user_name, attempts: req.session.attempts, grade: req.session.grade }) } } catch(e) { console.log("something broke: ", e); res.status(500).send(); } }) .catch(err => { console.log(err.message); console.log("hey couldn't find user"); req.session.error = new ErrorHandler(404, 'Incorrect email or last name'); res.render('login_user', { //err: err (or if same name to this) //err error: req.session.error }); }) }
I tried Lucas suggestion, and got this error:
C:usersdanieldocumentsgitusa-govt-quizcontrollersloginUser.js:41 const isValidPsw = await bcrypt.compare(password, user['Password']); ^^^^^ SyntaxError: await is only valid in async function at wrapSafe (internal/modules/cjs/loader.js:979:16) at Module._compile (internal/modules/cjs/loader.js:1027:27) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Module.require (internal/modules/cjs/loader.js:952:19) at require (internal/modules/cjs/helpers.js:88:18) at Object.<anonymous> (C:usersdanieldocumentsgitusa-govt-quizserver.js:5:29) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) [nodemon] app crashed - waiting for file changes before starting...
Any other ideas?
Advertisement
Answer
I was able to resolve this by putting a “async” in front of rows. This seems strange to me, and I don’t know if this is normal, but it works.
Here’s my final code from what I modified:
.then(async rows => { const user = rows.find( user => user['User_Name'] === user_name); if (user == null) { return res.status(400).send('Cannot find user'); } try { if (await bcrypt.compare(password, user['Password'])) { console.log('Password: ', password, 'hashedPassword: ', hashedPassword); console.log("this is a loginUser: "); console.log(req.session); //return res.redirect('questions') return res.render('user-progress', { userName: req.session.user_name, attempts: req.session.attempts, grade: req.session.grade }) } } catch(e) { console.log("something broke: ", e); res.status(500).send(); } }) .catch(err => { console.log(err.message); console.log("hey couldn't find user"); req.session.error = new ErrorHandler(404, 'Incorrect email or last name'); res.render('login_user', { //err: err (or if same name to this) //err error: req.session.error }); }) }