简体   繁体   English

MySql、NodeJS、ExpressJS 和 bcrypt:处理用户登录的最佳方式是什么?

[英]MySql, NodeJS, ExpressJS and bcrypt: what is the best way to handle users' login?

My solution works, but I'm not sure this is safe and appropriate.我的解决方案有效,但我不确定这是安全和适当的。 On the front end I have a ReactJS app that send with axios a request with the login and password.在前端,我有一个 ReactJS 应用程序,它使用 axios 发送带有登录名和密码的请求。 On the back end I have NodeJS + ExpressJS handling the request as follows:在后端,我让 NodeJS + ExpressJS 处理请求,如下所示:

router.post('/', function(req, res, next) {
  // get the records that match the login provided
  const sql = "SELECT name, surname, login, password, blocked FROM users WHERE login=?";
  query(sql, [req.body.login])
  .then((result)=> {
    // if there are 1 or more results, compare the passwords with bcrypt
    if (result.length > 0) {
      bcrypt.compare(req.body.password, result[0].password, function(err, success) {
        if (success) {
          // if the user is not blocked, send the status 200 with user's data
          result[0].blocked ?
            res.status(401).json({type: 'Warning', message: 'Your account has been blocked. Plase contact the admins.'})
            :
            res.status(200).json({name: result[0].name, surname: result[0].surname, email: result[0].email});
        } else {
          // send an error if the password is wrong
          res.status(401).json({type: 'Error', message: 'Please check that your login and password are correct.'});
        } 
      });
    } else {
      // send an error if the login was not found
      res.status(401).json({type: 'Error', message: 'Please check that your login and password are correct.'});
    }
  }); 

});

Is it enough/safe to query the db for the provided login (it's unique) with if (result.length > 0) ?使用if (result.length > 0)查询数据库以获取提供的登录名(它是唯一的)是否足够/安全?

Is it ok to have the error message contained in the server response like this?可以在这样的服务器响应中包含错误消息吗?

res.status(401).json({type: 'Warning', message: 'Your account has been blocked. Plase contact the admins.'})

I have the chance to let the user know if he typed the correct login but the wrong password;我有机会让用户知道他是否输入了正确的登录名但输入了错误的密码; should I let him know that?我应该让他知道吗? I think it would give to malicious users the knowledge that the login actually exists, so for now I just send a generic login/pwd error.我认为它会让恶意用户知道登录确实存在,所以现在我只发送一个通用的登录/密码错误。 Is this ok?这个可以吗?

Is ok to send the user's data from the server to the client if the login was successful?如果登录成功,可以将用户的数据从服务器发送到客户端吗?

Is it ok to have the error message contained in the server response like this?可以在这样的服务器响应中包含错误消息吗?

I have the chance to let the user know if he typed the correct login but the wrong password;我有机会让用户知道他是否输入了正确的登录名但输入了错误的密码; should I let him know that?我应该让他知道吗? I think it would give to malicious users the knowledge that the login actually exists, so for now I just send a generic login/pwd error.我认为它会让恶意用户知道登录确实存在,所以现在我只发送一个通用的登录/密码错误。 Is this ok?这个可以吗?

Your implementation is good enough.你的实现已经足够好了。 It's also a good practice letting users know why they are unable to login without giving out too much information EVEN when it's a problem with their supplied credentials (something you are doing already).这也是一个很好的做法,让用户知道为什么他们无法登录而不提供太多信息,即使他们提供的凭据有问题(你已经在做的事情)。

Is it enough/safe to query the db for the provided login (it's unique) with if (result.length > 0)?使用 if (result.length > 0) 查询数据库以获取提供的登录名(它是唯一的)是否足够/安全?

  • Yes, this is fine too.是的,这也很好。 You may also want to add a LIMIT 1 to your query to give you a little performance boost since there is no point having your DB scan through all the records when you expect only one result.您可能还想在查询中添加一个LIMIT 1以提高性能,因为当您只期望一个结果时,让您的数据库扫描所有记录是没有意义的。

It is also a good practice to only send the minimum amount of information and request for more on demand.最好只发送最少量的信息并按需请求更多信息。

As a general observation of your code, you would benefit from the following:作为对代码的一般观察,您将受益于以下内容:

  • Doing some error checking on your request object before querying the database at all (good practice too) as there is no guarantee that a valid or well formatted username/password would be sent with the request.在查询数据库之前对您的请求 object 进行一些错误检查(也是很好的做法),因为不能保证会随请求一起发送有效或格式正确的用户名/密码。

  • Moving the responses into another file to make your code cleaner and maintainable.将响应移动到另一个文件中以使您的代码更清晰和可维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM