代码之家  ›  专栏  ›  技术社区  ›  Filipe Ferminiano

passportJS中从未使用Google策略调用反序列化用户函数

  •  0
  • Filipe Ferminiano  · 技术社区  · 6 年前

    这是我的护照.js配置:

    ...
    passport.serializeUser(function(user, done) {
    
            done(null, user.id);
    
        });
    
        passport.deserializeUser(function(sessionUser, done) {
    
          User.findById(sessionUser, function(err, user) {
                 done(err, user);
             });
      });
    
    passport.use('google', new GoogleStrategy({
            clientID        : process.env.GOOGLE_CLIENTID,
            clientSecret    : process.env.GOOGLE_CLIENTSECRET,
            callbackURL     : process.env.GOOGLE_CALLBACKURL,
            passReqToCallback: true,
        },
            function(req, token, refreshToken, profile, done) {
    
    
                process.nextTick(function() {
    
                    // console.log(profile);
    
                    var values = { 
                        where: { google_id: profile.id }, 
                        defaults: {google_id: profile.id, name: profile.displayName} 
                    };
    
                    User.findOrCreate(values)
                    .spread(function(user, created) {
                        return done(null,user);
                    });
                });
            }
        ));
    

    以下是我的路线:

    app.get('/auth/google', 
        passport.authenticate('google', { scope : ['profile', 'email'] }));
    
    
    // the callback after google has authenticated the user
    app.get('/auth/google/callback',
            passport.authenticate('google', {
                    successRedirect : '/portfolio/crypto',
                    failureRedirect : '/'
            }));
    

    因为从来没有调用反序列化用户,需求用户从未定义。我怎样才能解决这个问题?

    0 回复  |  直到 6 年前