Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing Login method in the Local Callback #35

Closed
scott-wyatt opened this issue Apr 8, 2015 · 7 comments
Closed

Missing Login method in the Local Callback #35

scott-wyatt opened this issue Apr 8, 2015 · 7 comments
Assignees

Comments

@scott-wyatt
Copy link
Member

I'm having an issue trying to use login. I would just add this in and do a PR, but I am really bad at doing the testing stuff. I think this is what it should look like... perhaps I'm wrong?

callback: function (req, res, next) {
    var provider = req.param('provider', 'local');
    var 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 === 'login' && !req.user) {
        this.protocols.local.login(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);
      }
    }
  }
@tjwebb
Copy link
Member

tjwebb commented Apr 8, 2015

I'm having an issue trying to use login.

Can you describe in more detail what you tried to do, and what didn't seem to work?

@scott-wyatt
Copy link
Member Author

Thanks @tjwebb for the quick reply. When I use the route 'auth/local/:callback' by calling 'auth/local/login' it should hit the service/protocols/local.js login export, however when the request get's routed through services/passport.js the login action is missing, causing it to return "Invalid Action." I could be wrong, but in the current code base, there is no way to preform a passport local login which should look like this:

     else if (action === 'login' && !req.user) {
        this.protocols.local.login(req, res, next);
      }

@tjwebb tjwebb self-assigned this Apr 8, 2015
@scott-wyatt
Copy link
Member Author

I can verify that this works:

// api/services/passport.js

var _ = require('lodash');
var _super = require('sails-permissions/api/services/passport');

function passport () { }

passport.prototype = Object.create(_super);
_.extend(passport.prototype, {

  // Extend with custom logic here by adding additional fields and methods,
  // and/or overriding methods in the superclass.

  /**
   * For example:
   *
   * foo: function (bar) {
   *   bar.x = 1;
   *   bar.y = 2;
   *   return _super.foo.call(this, bar);
   * }
   */


  callback: function (req, res, next) {
    var provider = req.param('provider', 'local');
    var 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 === 'login' && !req.user) {
        this.protocols.local.login(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);
      }
    }
  }

});

module.exports = new passport();

It just overrides the default callback method when sails-auth is used with sails-permissions. I hope that helps in your investigation.

@bgulotta
Copy link
Contributor

bgulotta commented Apr 9, 2015

Hey Scott...The unit tests in this file show you how to authenticate: https://github.com/tjwebb/sails-auth/blob/master/test/unit/controllers/AuthController.test.js

@scott-wyatt
Copy link
Member Author

Oh snap @bgulotta thanks man, I'll give it a go.

@scott-wyatt
Copy link
Member Author

@bgulotta is correct, you can just point a login form to /auth/local and login works. My initial assumption was wrong about the missing login function.

@fubaydullaev
Copy link

Hi guys !

I wonder when action becomes "login" as it sends me undefined all the time ?

var action = req.param('action');

if (action === 'register') {
  res.redirect('/register');
}
else if (action === 'login') {
  res.redirect('/login');
}
else if (action === 'disconnect') {
  res.redirect('back');
}
else {
  // make sure the server always returns a response to the client
  // i.e passport-local bad username/email or password
  res.forbidden();
}

And this logic works in sails-generate-auth and it's a bit different:

switch (action) {
  case 'register':
    res.redirect('/register');
    break;
  case 'disconnect':
    res.redirect('back');
    break;
  default:
    res.redirect('/login');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants