Sailsjs Login API

I have a sailsjs project that only has 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 PUT to POSTMAN to: http: // localhost: 1337 / login (where the server is running) I get a 404 Not Found ERROR error.

Any ideas?

+3


source to share


2 answers


This is correct as it responds with res.notFound (); (404) if this is incorrect and the password is indeed incorrect.



+3


source


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

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

      



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

+1


source







All Articles