节点 js + 登录 API

node js + login API

我正在尝试使用 bcrypt 对密码进行哈希处理以 API 登录 将散列值与原始密码进行比较时出现的问题总是给我输出 Auth failed1 while username is correct and password is the same 。 提前致谢


router.post('/login', (req, res, next) => {
const user1 = {
        Username: req.body.Username,
        Password: req.body.Password
    };
    var sql = "SELECT * FROM Customer WHERE Username  = '" + user1.Username + "'";
    db.executeSql(sql, function (data, err) {
        if (err) {
            httpMsgs.show500(req, res, err);
        } else {
            if (isEmptyObject(data)) {
                return res.status(401).json({
                    message: 'Auth Failed'
                });
            } else {
                bcrypt.compare(user1.Password, data.Password, (err, result) => {
                    if (err) {
                        return res.status(401).json({
                            message: "Auth failed1"
                        });
                    }
                    if (result) {
                        return res.status(200).json({
                            message: "Auth successful"
                                        });
                    }
                    res.status(401).json({
                        message: "Auth failed2"
                    });
                });
            }
        }
    });
});

这里是查询的响应,数据变量包含在唯一用户情况下的记录数组,它将具有长度为 1 的数组。

输入 data[0].Password 应该可以。

更好的方法是获取用户对象而不是数组作为响应。因为我们不应该在好的生产代码中有data[0]