Jabaa
Jabaa

Reputation: 1763

Passport.js authentication with node.js

I am from laravel background trying to implement passport.js authentication in my sails.js app

the documentation is minimal and also hard to understand

Here is my login.js

module.exports = {
    /*
     *Normal login
     */
    login:function (req,res) {
      //user input  
      console.log('email'+req.parm('email')+'password'+req.param('password'))
        passport.authenticate(
            'local-login',
            function (req,res) {


    }
}



 passport.use('local-login',new LocalStrategy(function (username,password,done) {
    if(username=='test')
        return done(null,username);
    else
        return done(null,false,{message:'Invalid userinfo'})
}));

but the passport.authenticate never fired

From their documentation

app.post('/login',
  passport.authenticate('local'),
  function(req, res) {
    // If this function gets called, authentication was successful.
    // `req.user` contains the authenticated user.
    res.redirect('/users/' + req.user.username);
  });

also what is the meaning of this If this function gets called, authentication was successful.

Found this tutorial http://iliketomatoes.com/implement-passport-js-authentication-with-sails-js-0-10-2/ but its explanation so poor

Upvotes: 0

Views: 331

Answers (1)

noah-sd
noah-sd

Reputation: 186

The tutorial is pretty old as it's for sails 0.10, but it is still valid. You are using the passport-local strategy. When you define your strategy you've got an extra parameter. Remove this 'local-login' parameter.

You currently have:

passport.use('local-login',new LocalStrategy(function...

Replace the above with:

passport.use(new LocalStrategy(function...

Then when you call authenticate specify 'local' not 'local-login' as the strategy, so you have:

passport.authenticate(
  'local',
  function (req,res)...

'local' goes with passport-local. If you we're using the passport-http-bearer strategy then you would call

passport.authenticate('bearer', function...

I usually put my strategy definitions in /config/bootstrap.js along with the session serialization and helpers for finding the user. Then my controller-->service makes the passport.authenticate call.

Upvotes: 1

Related Questions