2014-11-06 38 views
0

我想使用req.flash(「message」:error)來支持SailsJS中passportJS的錯誤消息回調。然而,PassportJS不回調過程中處理如下:(類似的帖子:PassportJS Custom Authenticate Callback Not Calledreq(請求)未在sailsJS中爲passportjs.use定義

//there is no req found 
req.flash("message" , "invalid password"); 

這通常是正常的,如果有這樣的:

function(req, res, next) { 
    passport.authenticate('local', function(err, user, info) { 
    //.... 
    req.flash("message" , "invalid password"); 
} 

但我不能用它裏面的護照。使用。

/services/passport.js 
passport.use(new HttpBasicStrategy(
    function(username, password, done) { 
    // asynchronous verification, for effect... 
    process.nextTick(function() { 
     // Find the user by username. If there is no user with the given 
     // username, or the password is not correct, set the user to `false` to 
     // indicate failure. Otherwise, return the authenticated `user`. 
     findByUsername(username, function(err, user) { 
     if (err) 
      return done(null, err); 
     if (!user) { 
      return done(null, false, { 
      message: 'Unknown user ' + username 
      }); 
     } 
     bcrypt.compare(password, user.password, function (err, res) { 
      if (!res){ 

     --> //there is no req found 
     --> req.flash("message" , "invalid password"); 

      return done(null, false, { 
       message: 'Invalid Password' 
      }); 
      } 
      var returnUser = { 
      username: user.username, 
      createdAt: user.createdAt, 
      id: user.id 
      }; 
      return done(null, returnUser, { 
      message: 'Logged In Successfully' 
      }); 
     }); 
     }) 
    }); 
    } 
)); 

是否有另一種方法可以調用req.flash?我在快遞和sailsjs方面很新,請原諒我的無知。

回答

1

對於帆v0.10.x sails-generate-auth是完美的。

+0

感謝您的建議。我已經嘗試過,但它沒有我需要的。 (護照-HTTP)。 – holyxiaoxin 2014-11-07 02:15:41

+0

其實,它也支持passport-http ...我使用這個https://github.com/jaredhanson/passport-http-bearer編輯:不知道passport-http是否類似於passport-http-持有者... – Melvin 2014-11-07 02:23:10

+0

它不,我建議作者做一個。但在那之前我無法真正使用它。而且我使用的是相當定製的護照版本,需要更新回調。我也可以自己定製護照。 (: – holyxiaoxin 2014-11-07 02:28:19