Coder Social home page Coder Social logo

Comments (7)

tjwebb avatar tjwebb commented on July 29, 2024

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?

from sails-auth.

scott-wyatt avatar scott-wyatt commented on July 29, 2024

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

from sails-auth.

scott-wyatt avatar scott-wyatt commented on July 29, 2024

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.

from sails-auth.

bgulotta avatar bgulotta commented on July 29, 2024

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

from sails-auth.

scott-wyatt avatar scott-wyatt commented on July 29, 2024

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

from sails-auth.

scott-wyatt avatar scott-wyatt commented on July 29, 2024

@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.

from sails-auth.

fubaydullaev avatar fubaydullaev commented on July 29, 2024

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

from sails-auth.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.