2

我在sails.js中使用passport-localpassport-facebook驗證策略。使用電子郵件進行身份驗證正常。但是,當用戶使用Facebook進行身份驗證時,我收到此錯誤消息[Error: Failed to serialize user into session]使用passport-facebook策略與本地護照無法序列化用戶到會話中

然後我測試了serializeUser方法,結果發現user param在facebook的情況下是空的。雖然我也試圖看看verifyHandler是否被調用,它不被調用。

這裏是我的Facebook的身份驗證操作代碼:

facebook: function (req, res) { 

    passport.authenticate('facebook', {failureRedirect: '/login', scope: ['email']}, function (err, user) { 

     if ((err) || (!user)) { 
     req.session.flash = { 
      errMsg: 'Email or password mismatch.' 
     } 

     return res.redirect('/login'); 
     } 

     req.logIn(user, function (err) { 
     if (err) { 
      console.log(err); 
      res.view('500'); 
      return; 
     } 

     res.redirect('/'); 
     return; 
     }); 
    })(req, res); 

    } 

這是passport.js服務(API /服務/ passport.js)

var passport = require('passport'), 
    LocalStrategy = require('passport-local').Strategy, 
    FacebookStrategy = require('passport-facebook').Strategy, 
    bcrypt = require('bcrypt'); 


var verifyHandler = function (token, tokenSecret, profile, done) { 

    console.log('in verifyHandler'); // this line is not being executed. 
    console.log(profile); 

    process.nextTick(function() { 

    User.findOne({uid: profile.id}, function (err, user) { 

     if (err) { 
     return done(err); 
     } 

     if (user) { 

     return done(null, user); 

     } else { 

     var data = { 
      provider: profile.provider, 
      uid: profile.id, 
      name: profile.displayName 
     }; 

     if (profile.emails && profile.emails[0] && profile.emails[0].value) { 
      data.email = profile.emails[0].value; 
     } 

     User.create(data, function (err, user) { 
      return done(err, user); 
     }); 
     } 
    }); 
    }); 
}; 

passport.serializeUser(function (user, done) { 
    done(null, user.id); 
}); 

passport.deserializeUser(function (id, done) { 
    User.findById(id, function (err, user) { 
    done(err, user); 
    }); 
}); 

passport.use(new LocalStrategy({ 
    usernameField: 'email', 
    passwordField: 'password' 
    }, 
    function (email, password, done) { 

    User.findOne({email: email}).exec(function (err, user) { 
     if (err) { 
     return done(err); 
     } 
     if (!user) { 
     return done(null, false, {message: 'Unknown user ' + email}); 
     } 

     bcrypt.compare(password, user.password, function (err, res) { 
     if (!res) return done(null, false, {message: 'Invalid Password'}); 
     return done(null, user); 
     }); 

    }); 
    } 
)); 

passport.use(new FacebookStrategy({ 
    clientID: sails.config.facebook.clientID, 
    clientSecret: sails.config.facebook.clientSecret, 
    callbackURL: sails.config.facebook.callbackURL 
}, verifyHandler)); 

最後的代碼(配置/ passport.js)

var passport = require('passport'), 
    LocalStrategy = require('passport-local').Strategy, 
    FacebookStrategy = require('passport-facebook').Strategy; 

module.exports = { 
    http: { 
    customMiddleware: function (app) { 
     app.use(passport.initialize()); 
     app.use(passport.session()); 
    } 
    } 
}; 

有什麼想法?

回答

0

檢查user.id是否定義,它是字符串,但不是ObjectId()。

passport.serializeUser(function (user, done) { 
    done(null, user.id); 
}); 

相關問題