Previously, my code was functioning correctly. However, it now seems to be broken for some unknown reason. I am using MariaDB
as my database and attempting to compare passwords. Unfortunately, I keep encountering an error that says "
Unexpected Identifier on "bcrypt" after await
". When I remove the await
, the code works but fails to validate the password even if incorrect. Can someone please guide me on where I might be going wrong here? Thank you.
Edit: I realized that I had omitted the user info after .then. It appears that I may have accidentally deleted it while making some comments when posting this question, so I've added it back in. Below is the updated 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 followed Lucas' suggestion, however, I encountered a new error:
C:\users\daniel\documents\git\usa-govt-quiz\controllers\loginUser.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:\users\daniel\documents\git\usa-govt-quiz\server.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 suggestions would be greatly appreciated.