Sailsjs login API



我有一个sailsjs项目,只有用户(电子邮件和密码)需要验证。

代码如下:

routes.js

'PUT /login': 'UsersController.login'

UsersController.js:

login: function (req, res) {
    // Try to look up user using the provided email address
    Users.findOne({
      correo: req.param('correo')
    }, function foundUser(err, user) {
      if (err) return res.negotiate(err);
      if (!user) return res.notFound();
      // Compare password attempt from the form params to the encrypted password
      // from the database (`user.password`)
      require('machinepack-passwords').checkPassword({
        passwordAttempt: req.param('password'),
        encryptedPassword: user.encryptedPassword
      }).exec({
        error: function (err){
          return res.negotiate(err);
        },
        // If the password from the form params doesn't checkout w/ the encrypted
        // password from the database...
        incorrect: function (){
          return res.notFound();
        },
        success: function (){
          // Store user id in the user session
          req.session.me = user.id;
          // All done- let the client know that everything worked.
          return res.ok();
        }
      });
    });
  }

如果我在sailsjs中访问它,在控制器中使用以下命令可以正常工作:

$http.put('/login', {
    correo: $scope.loginForm.correo,
    password: $scope.loginForm.password
  })

但如果我尝试做一个PUT在邮差到:http://localhost:1337/login(服务器正在运行的地方),我得到一个404 Not Found错误。

任何想法?

它是正确的,因为它响应res.notFound();(404)如果不正确,密码确实不正确。

我认为在帆函数中,当你做post, put和传递JSON对象时,它被

访问
var attributes=req.body
Users.findOne({correo:attributes['correo']})

之类的东西,req.params(")是用于GET参数的,如果不是这样,我们可以看看你在POSTMAN中做了什么吗?

最新更新