SailsJs Passport authentication for Android

217 Views Asked by At

I am trying to use Passport for authentication in a SailsJs project. I used sails-generate-auth to generate a template. The registration process works well when using a browser to send http requests the Sails server.

However, when trying registering an user from an Android device, it is not working.

The basic flow based on my understanding is: 1) user inputs registration info on the Android device; 2) Android device sends http request to Sails server; 3) The Sails server registers the user and, if successful, sends an access token back to the Android device, together with user info and a user id. 4) In all subsequent http requests, the Android device includes the access token.

To implement step 2, Android device packs data in the body of a http POST request and send it to the following,

post /auth/local/register

similar to what I did when using a browser. However, the server prints error that req.login in AuthController.js is undefined.

I could not understand the reason that req from browser has login method while req from Android device has no such method?

Relevant code is listed below:

In config/routes.js

'post /auth/local': 'AuthController.callback',
'post /auth/local/:action': 'AuthController.callback',

In api/controllers/AuthController.js

callback: function (req, res) {


  function tryAgain (err) {
     //Some logics here. Omitted.  
  }

  passport.callback(req, res, function (err, user) {

    if (err) {
      return tryAgain();
    }

    req.login(user, function (err) {
      if (err) {
        return tryAgain();
      }

      var userID = user.id;
      Passport.find({user: userID}, function(err, items){
        if(err)
          return err;
        res.json({userData: user, token: items[0].accessToken});
      });
    });
  });
}

In api/services/passport.js

passport.callback = function (req, res, next) {
  var provider = req.param('provider') || 'local'
    , action   = req.param('action');

  // Passport.js wasn't really built for local user registration, but it's nice
  // having it tied into everything else.
  if (provider === 'local' && action !== undefined) {
    if (action === 'register' && !req.user) {
      this.protocols.local.register(req, res, next);
    }
    else if (action === 'connect' && req.user) {
      this.protocols.local.connect(req, res, next);
    }
    else if (action === 'disconnect' && req.user) {
      this.protocols.local.disconnect(req, res, next);
    }
    else {
      next(new Error('Invalid action'));
    }
  } else {
    if (action === 'disconnect' && req.user) {
      this.disconnect(req, res, next) ;
    } else {
      // The provider will redirect the user to this URL after approval. Finish
      // the authentication process by attempting to obtain an access token. If
      // access was granted, the user will be logged in. Otherwise, authentication
      // has failed.
      this.authenticate(provider, next)(req, res, req.next);
    }
  }
};
0

There are 0 best solutions below