Question

I'm trying to implement passport-local with sailsjs, and I am not getting any response when trying to authenticate. Here is the code:

//config/passport.js

var  passport = require('passport')
    , LocalStrategy = require('passport-local').Strategy;

module.exports = {
    express: {
        customMiddleware: function(app){
            console.log('Express midleware for passport');
            app.use(passport.initialize());
            app.use(passport.session());
        }
    }
};

passport.serializeUser(function(user, done) {
    done(null, {omar: "omar"});
});

passport.deserializeUser(function(id, done) {

        done(null, {omar: "omar"});

});

passport.use(new LocalStrategy(
    function(username, password, done) {
        // asynchronous verification, for effect...
        process.nextTick(function () {

                return done(null, {user: "omar"});

        });
    }
));

_

//LoginController.js
var passport = require('passport');
var loginController = {


    login: function(req, res){
        //res.json(req.param('username'));
        console.log('loginctrl');

        passport.authenticate('local', { failureRedirect: '/login', failureFlash: true }),
            function(req, res) {
                console.log('passport auth');
                res.send("success");
            }


    },

  /**
   * Overrides for the settings in `config/controllers.js`
   * (specific to LoginController)
   */
  _config: {}


};


module.exports = loginController;

I'm trying to POST to /login, but the only console messages I get are:

Express midleware for passport
loginctrl

Meaning It's not calling LocalStrategy callback and I get "0 NO RESPONSE" error..
Why is that?

Was it helpful?

Solution

There's something wrong with the way you call passport.authenticate here:

passport.authenticate('local', { failureRedirect: '/login', failureFlash: true }),
            function(req, res) {
                console.log('passport auth');
                res.send("success");
            }

passport.authenticate returns a function that is supposed to be called on request / response.

See here for some examples of Sails/Passport integration:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top