Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 身份验证节点JS Sequelize Express_Mysql_Node.js_Express_Authentication_Sequelize.js - Fatal编程技术网

Mysql 身份验证节点JS Sequelize Express

Mysql 身份验证节点JS Sequelize Express,mysql,node.js,express,authentication,sequelize.js,Mysql,Node.js,Express,Authentication,Sequelize.js,我正在尝试用express、sequelize和mysql实现NodeJS身份验证,但我被阻止了。我通过webservice获得登录名和密码值。我想将它们与数据库值匹配: app.get('/login', function (req, res, err) { var whereUser = { login: req.query.login, password: req.query.password }

我正在尝试用express、sequelize和mysql实现NodeJS身份验证,但我被阻止了。我通过webservice获得登录名和密码值。我想将它们与数据库值匹配:

app.get('/login', function (req, res, err) {           
    var whereUser = {
        login: req.query.login,
        password: req.query.password
    }      

    if (!req.query.login || !req.query.password) {
        res.send('login failed');
//Here my code is wrong ! 
I try to compare login and password values with database login and passwword values          
            } else if (req.query.login && req.query.password == UsrPerson.findOne({ where: whereUser })) {        
                console.log("auth ok")
                req.session.user = "amy";
                req.session.admin = true;        
                res.send("login success!");
            } else {
                console.log("ERROR")
                res.send(err)
            }
        });
我该怎么做?多谢各位

app.get('/login', function (req, res, err) {    
  const { login, password } = req.query;

  UsrPerson
    .findOne({
      where: {
        login: login,
        password: password
      }
    })
    .then((foundUser) => {
      if(!foundUser){
        res.send('login failed');
      } else {
        console.log("auth ok");
        req.session.user = "amy";
        req.session.admin = true;
        res.send("login success!");
      }
    })
    .catch((err) => {
      console.log('ERROR');
      res.send(err);
    });
});
您希望比较具有给定用户名和密码组合的用户是否存在

而且似乎您正在以纯文本形式存储密码,而不进行任何加密。这根本不安全。您必须使用类似的库,并且只在数据库中存储加密的密码

您希望比较具有给定用户名和密码组合的用户是否存在


而且似乎您正在以纯文本形式存储密码,而不进行任何加密。这根本不安全。您必须使用类似的库,并且只将加密密码存储在数据库中

sequelize中的
findOne
方法返回模型的实例对象。 这意味着您无法将
密码
与实例进行比较

第二个问题是
findOne
方法是异步的,您需要
wait
并使用
async
方法

app.get('/login', async function (req, res, err) {           
    var whereUser = {
        login: req.query.login,
        password: req.query.password
    }      

   if (!req.query.login || !req.query.password) {
       res.send('login failed');
    } else {        
        // The following code return an instance of the user if it was found.
       const user = await UsrPerson.findOne({ where: whereUser }))

       // If the user was not found that means the credentials was wrong.
       if (user) {
           console.log("auth ok")
           req.session.user = "amy";
           req.session.admin = true;        
           res.send("login success!");
       } else {
           console.log("ERROR")
           res.send(err)
       }
   } 
});

sequelize中的
findOne
方法返回模型的实例对象。 这意味着您无法将
密码
与实例进行比较

第二个问题是
findOne
方法是异步的,您需要
wait
并使用
async
方法

app.get('/login', async function (req, res, err) {           
    var whereUser = {
        login: req.query.login,
        password: req.query.password
    }      

   if (!req.query.login || !req.query.password) {
       res.send('login failed');
    } else {        
        // The following code return an instance of the user if it was found.
       const user = await UsrPerson.findOne({ where: whereUser }))

       // If the user was not found that means the credentials was wrong.
       if (user) {
           console.log("auth ok")
           req.session.user = "amy";
           req.session.admin = true;        
           res.send("login success!");
       } else {
           console.log("ERROR")
           res.send(err)
       }
   } 
});