如何让 bcrypt compare 承诺正常工作?
How to get bcyrpt compare in a promise to work right?
我以前用过这个,但现在由于某种原因坏了。我正在使用 MariaDB
作为数据库,需要比较密码,但它给我一个错误 Unexpected Identifier on "bcrypt" after await
。当我删除 await
时,它可以工作,但即使密码错误。我在这里做错了什么?谢谢
编辑:
我忘了在 .then 之后包含用户信息。
当我发布这个问题时,我必须删除它并附上一些评论。我加回来了。
这是我的代码:
//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
});
})
}
我尝试了 Lucas 的建议,得到了这个错误:
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...
还有其他想法吗?
我能够通过在行前面放置一个“异步”来解决这个问题。这对我来说很奇怪,我不知道这是否正常,但它确实有效。
这是我修改后的最终代码:
.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
});
})
}
我以前用过这个,但现在由于某种原因坏了。我正在使用 MariaDB
作为数据库,需要比较密码,但它给我一个错误 Unexpected Identifier on "bcrypt" after await
。当我删除 await
时,它可以工作,但即使密码错误。我在这里做错了什么?谢谢
编辑: 我忘了在 .then 之后包含用户信息。 当我发布这个问题时,我必须删除它并附上一些评论。我加回来了。 这是我的代码:
//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
});
})
}
我尝试了 Lucas 的建议,得到了这个错误:
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...
还有其他想法吗?
我能够通过在行前面放置一个“异步”来解决这个问题。这对我来说很奇怪,我不知道这是否正常,但它确实有效。
这是我修改后的最终代码:
.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
});
})
}