node js + 登录API

问题描述 投票:0回答:1

enter image description here我试图使用bcrypt登录API,在与原始密码进行哈希比较时,总是给我输出密码的问题。enter image description here Auth failed1而用户名正确,密码相同.thanks in advance.

s


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"
                    });
                });
            }
        }
    });
});
node.js api authentication bcrypt
1个回答
1
投票

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

data[0].Password 就可以了。

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

© www.soinside.com 2019 - 2024. All rights reserved.