2016-01-22 75 views
0

請設置護照ldapauth,它可以正常使用所有參數,問題在於如果用戶名或密碼錯誤,則根本不會執行進一步的驗證回調函數。它只是停止。由於這個原因,我無法給用戶反饋,以指出實際上錯誤的地方。有什麼線索我缺少什麼?這是結構Passport-ldapauth無法執行驗證回調

passport.use('ldapStudent', new LdapStrategy({ 
     usernameField: 'username', 
     passReqToCallback:true, 
     server: { 
      url: '..........', 
      bindDn: '.............', 
      bindCredentials: '..........', 
      searchBase: '..............', 
      searchFilter: '.............', 
      searchAttributes: ['givenName','sn'], 
      tlsOptions: { 
       ca: [fs.readFileSync('./ssl/server.crt', 'utf8')] 
      } 
     } 
    }, 
    function (req, user, done) { 
     //now check from the DB if user exist 

     if(user){ 

      //check if user email exist; 
      User.findOne({'EmailAddress': user}, function (err, userdata) { 
       // In case of any error, return using the done method 
       if (err) 
        return done(err); 
       //user exist redirect to home page and send user object to session 
       if (userdata) { 
        //userActivity(PostActivity); 
        console.log(userdata); 
        return done(null, userdata); 
       }else { 
        //new user, add them to the user model 
        var newUser = new User(); 
        newUser.EmailAddress = req.body.username, 
         newUser.JoinedDate = Date.now(), 
         newUser.UserType = 'Student' 
        newUser.save(function (err, result) { 
         if (err) { 
          console.log('Error in Saving NewUser: ' + err); 
         } else { 
          console.log(result); 
          var PostActivity = { 
           ActivityName: req.res.__('Student Joined'), 
           ActivityDate: Date.now(), 
           UserID: result._id, 
           UserIP: (req.header('x-forwarded-for') || req.connection.remoteAddress) + ' Port: ' + req.connection.remotePort 
          }; 
          userActivity(PostActivity); 
          console.log('User Registration successful'); 
          return done(null, newUser, req.flash('SuccessMessage', req.res.__('You have been successfully Registered'))); 
         } 
        }) 
       } 
      }); 

     }else{ 
      return done(null, false, req.flash('ValidationError', req.res.__('Wrong password and/or email address'))); 

     }})); 

這是我實際做的登錄

router.post('/login', passport.authenticate('ldapStudent', { 
    successRedirect: '/', 
    failureRedirect: '/userlogin', 
    failureFlash: true 
})); 

代碼工作很好,就像我期望的那樣,故意省略了LDAP選項,對象的參數。 問題是當用戶憑證不正確,驗證回調根本沒有得到執行,所以我無法返回一條閃爍消息讓用戶知道發生了什麼

回答

0

passport-ldapauth不會執行驗證回調如果沒有任何證據證明這種情況,如果證書不正確並且用戶沒有收到。總的來說,這些策略是如何運作的,例如passport-local如果username or password is missing不執行驗證回調。

策略,passport-ldapauth包括在內,通常還包含一個(可配置的)故障閃存消息。列出了passport-ldapauth的一般可配置登錄失敗消息in the documentation。每一個消息也有一個默認值,因此,即使沒有配置故障提示信息設置(當然,你必須flash middleware in use給出)

而且,你應該在驗證回調使用req.flash()功能,但supply an info message