Sailsjs login API

1k Views Asked by At

I have a sailsjs project that has only users ( email and password ) to authenticate.

Here's the code:

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();
        }
      });
    });

  }

If I access it in sailsjs it works fine using the following command in the controller:

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

but if I try to do a PUT in POSTMAN to: http://localhost:1337/login (where the server is running) I get a 404 Not Found ERROR.

Any ideas?

2

There are 2 best solutions below

0
On BEST ANSWER

It is correct as it is responding with a res.notFound(); (404) if incorrect and the password was indeed incorrect.

0
On

I think in sails functions, when you do post, put, and pass a JSON object, it is accessed by

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

something like that, the req.params('') is for GET parameters, if it is not the case, could we see what you did in POSTMAN exactly?