2017-06-14 130 views
0

我從laravel背景想在我sails.js的應用程序來實現passport.js authentication與node.js的Passport.js認證

的文件是最小的,也很難理解

這裏是我的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'}) 
})); 

passport.authenticate從來沒有發射

從他們做cumentation

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

也是這個又是什麼If this function gets called, authentication was successful.

發現這個教程http://iliketomatoes.com/implement-passport-js-authentication-with-sails-js-0-10-2/但其解釋如此之差

回答

1

本教程的含義是很老的,因爲它是爲帆0.10,但它仍然是有效的。您正在使用護照本地策略。當你定義你的策略時,你有一個額外的參數。刪除這個'local-login'參數。

您目前有:

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

替換上面:

passport.use(new LocalStrategy(function... 

然後當你調用驗證指定的「本地」而不是「本地登錄」的策略,所以你必須:

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

'本地'與護照本地。如果我們使用的護照HTTP承載的策略,那麼你會打電話

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

我通常把我的策略定義在/config/bootstrap.js與會話序列化和助手查找用戶一起。然後我的控制器 - >服務進行passport.authenticate調用。