2013-12-10 14 views
2

我使用多種策略與護照(本地和承載策略)。本地策略的登錄工作。我們在登錄後生成一個令牌,令牌存儲在redis中。在初始登錄後,只要在redis中找到令牌,我就想使用不帶會話的承載認證。如果我發送正確的令牌,我可以查詢redis並獲取用戶數據,但節點仍發送403響應,而不是我期望的200狀態碼。如果Redis的找不到令牌,帆崩潰,並顯示以下錯誤:Sails.js與護照-HTTP承載認證不起作用

/workspace/rs-api-sails/node_modules/redis/index.js:587 
      throw err; 
       ^
Error: Can't set headers after they are sent. 
    at ServerResponse.OutgoingMessage.setHeader (http.js:691:11) 
    at ServerResponse.res.setHeader (/workspace/rs-api-sails/node_modules/sails/node_modules/express/node_modules/connect/lib/patch.js:59:22) 
    at allFailed (/workspace/rs-api-sails/node_modules/passport/lib/passport/middleware/authenticate.js:153:13) 
    at attempt (/workspace/rs-api-sails/node_modules/passport/lib/passport/middleware/authenticate.js:232:28) 
    at Context.delegate.fail (/workspace/rs-api-sails/node_modules/passport/lib/passport/middleware/authenticate.js:227:9) 
    at Context.actions.fail (/workspace/rs-api-sails/node_modules/passport/lib/passport/context/http/actions.js:35:22) 
    at verified (/workspace/rs-api-sails/node_modules/passport-http-bearer/lib/strategy.js:125:19) 
    at /workspace/rs-api-sails/config/bootstrap.js:40:18 
    at try_callback (/workspace/rs-api-sails/node_modules/redis/index.js:580:9) 
    at RedisClient.return_reply (/workspace/rs-api-sails/node_modules/redis/index.js:670:13) 
10 Dec 13:25:15 - [nodemon] app crashed - waiting for file changes before starting... 

下面是bootstrap.js用於承載驗證碼:

passport.use(new BearerStrategy(
    function(token, done) { 
    var redis = require("redis"), 
    client = redis.createClient(null, null, {detect_buffers: true}); 

    client.get(token, function (err, reply) { 
     if (reply === null) { 
     // if token is not a key in redis, node throws the headers already sent error 
     return done(null, false); 
     } else { 
     User.findOne({ id: reply.toString() }).done(function(err, user) { 
      sails.log(user); 

      // here we get the user data from waterline but node still sends a 403 
      return done(null, user); 
     }); 
     } 
    }); 
    } 
)); 

此代碼是在政策/ isAuthenticated.js :

module.exports = function(req, res, next) { 
    var passport = require('passport');  

    passport.authenticate('bearer', { session: false })(req, res, next); 

    // User is allowed, proceed to the next policy, 
    // or if this is the last policy, the controller 
    if (req.isAuthenticated()) { 
    return next(); 
    } 

    // User is not allowed 
    // (default res.forbidden() behavior can be overridden in `config/403.js`) 
    return res.forbidden('You are not permitted to perform this action.'); 
}; 

我是新來的節點,非常感謝任何幫助!

回答

1

更新:似乎政策/ isAuthenticated.js一些變化後要現在的工作:

var passport = require('passport'); 

module.exports = function(req, res, next) { 

    passport.authenticate('bearer', { session: false }, function(err, user, info) { 

    if (req.isAuthenticated()) { 
     // user is allowed through local strategy 
     return next(); 
    } 

    if (err) { 
     return res.send(403, { error: 'Error: ' + info });   
    } 

    if (!user) { 
     return res.send(403, { error: 'Invalid token' }); 
    } 

    if (user) { 
     sails.log(user); 
     return next(); 
    } 

    // (default res.forbidden() behavior can be overridden in `config/403.js`) 
    return res.forbidden('You are not permitted to perform this action.');  

    })(req, res, next); 

};