2016-04-21 79 views
8

我對Node.js的開發很陌生,目前在空閒時間正在開發一個寵物項目。混合passport-facebook和passport-jwt的最佳方式是什麼?

到目前爲止,我已使用passportpassport-jwt創建了JWT身份驗證,並將其用於所有RESTful API。

現在我正在考慮將這種混合與某種Facebook身份驗證混合使用,仍然希望使用令牌身份驗證。

目前,這是怎麼了產生和獲得令牌:

exports.authenticate = function(req, res) { 
    User.findOne({ 
     email: req.body.email 
    }, function(err, user) { 
     if (err) 
      return res.status(400).send(getErrorMessage(err)); 

     if (!user) { 
      res.status(400).send({ 
       success: false, 
       message: 'Authentication failed. User not found.' 
      }); 
     } else { 
      if (user.checkPassword(req.body.password)) { 

       let token = jwt.encode(user, config.secretPhrase); 

       res.json({ 
        success: true, 
        token: 'JWT ' + token 
       }); 
      } else { 
       res.status(401).send({ 
        success: false, 
        message: 'Authentication failed. Wrong password.' 
       }); 
      } 
     } 
    }); 
}; 

app.route('/api/users/authenticate') 
     .post(user.authenticate); 

,並驗證我做了以下內容:

let user = require('../../app/controllers/user-controller'); 
app.route('/api/todos') 
     .get(user.validateLogin, todos.list) 
     .post(user.validateLogin, todos.create); 

用戶控制器:

exports.validateLogin = passport.authenticate('jwt', { 
    session: false 
}); 

任何人可以建議一種乾淨的方式來混合這兩種策略?我應該使用express-jwt? express-jwt和passport-jwt有什麼區別?

回答

0

它看起來好像當前您正在將令牌發送回最終用戶。你如何挽救客戶端?我建議使用一個簡單的JWT庫,如jsonwebtoken,並將該標記設置爲httpOnly cookie。如果您當前將其保存在localStorage中,則可能更容易受到XSS攻擊。 這裏是我當前如何處理設置和檢查JWT針對用戶的要點是: https://gist.github.com/briancw/8c2021817c3bd34972e8e8deb6048a4f

0

你可以使用passport-facebook喜歡你做了什麼與passport-jwt新的策略,讓你可以保存Facebook的用戶令牌數據庫和退回您的代幣

相關問題