簡體   English   中英

使用passport.js與yeoman和grunt進行身份驗證

[英]Using passport.js with yeoman and grunt for authentication

我正在嘗試研究如何使用帶有grunt / yeoman的passport.js。 我有以下內容:

// at the top of my gruntfile.js
var passport = require('passport');
var BasicStrategy = require('passport-http').BasicStrategy;

passport.use(new BasicStrategy(
  function(username, password, done) {
    return done(null, true); // I would expect this to always succeed, but still challenge for credentials
  }
));

// further down in my connect config.
livereload: {
    options: {
        middleware: function (connect) {
            return [
                lrSnippet,
                passport.initialize(),
                passport.authenticate('basic', { session: false }),
                mountFolder(connect, '.tmp'),
                mountFolder(connect, yeomanConfig.app)
            ];
        }
    }
}

在每次請求時,響應都包含unauthorized 刪除對passport.authenticate的調用會使頁面正常工作,但顯然現在沒有身份驗證。 我已經嘗試過改變中間件的順序而且沒有幫助,而且我無法與yeoman / grunt的專家接近,所以我不完全確定還有什么可以嘗試...

任何幫助將不勝感激。

我在想你需要在BasicStrategy回調BasicStrategy一個對象傳遞給done() 我記得,護照JS使用這個對象來填充快遞應用程序中的req.user,因此我認為它可能期望一個object不是boolean

這是我在許多應用程序中使用的相同功能的更強大的示例:

  passport.use(new BasicStrategy(
    function(clientID, clientSecret, done) {
      AuthClient.findOne({ clientID: clientID }, function(err, client) {
        if (err) { return done(err); }
        if (!client) { return done(null, false); }
        if (client.secret != clientSecret) { return done(null, false); }
        return done(null, client);
      });
    }
  ));

正如您所看到的,BasicStrategy用於分析客戶端ID和clientSecret,它相當於您的用戶名/密碼組合。 因為你實際上並沒有像我的例子中所示那樣從數據庫中提取它,我希望如果你只是按照上面的建議並將{}傳遞給done(null, {}) ,它可能會更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM