【问题标题】:Passport for Google oauth20, middleware not in use, passport.initialize()Google oauth20 的 Passport,未使用中间件,passport.initialize()
【发布时间】:2018-07-04 15:16:54
【问题描述】:

我正在使用 passport-google-oauth20、mongoose、mlab 进行用户身份验证。 一旦我从 google auth 收到回调,我会在 done mehhod 中收到以下错误:

UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝 id:3):错误:passport.initialize() 中间件未使用

我附上了我的代码库的屏幕截图。 提前致谢!

const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const mongoose = require('mongoose');
const Keys = require('../config/dev');
const User = mongoose.model('users');

passport.serializeUser((user, done) => {
  done(null, user.id);
});
passport.deserializeUser((id, done) => {
  User.findById(id).then(user => {
    done(null, user);
  });
});
passport.use(new GoogleStrategy({
    clientID: Keys.GOOGLE_CLIENT_ID,
    clientSecret: Keys.GOOGLE_CLIENT_SECRETKEY,
    callbackURL: "/auth/google/callback"
  },(accessToken, refreshToken, profile, done) => {
        User.findOne({googleID: profile.id}).then((existingUser) => {
            if(existingUser){
                console.log('existing');
                //This above log is printing fine and then here I am getting error
                done(null, existingUser);
            } else{
                console.log('new');
                new User({googleID: profile.id}).save()
                .then((user) => {done(null, user);})
            }
        })
   }
));

【问题讨论】:

    标签: node.js express passport.js mlab passport-google-oauth


    【解决方案1】:

    请查看passport-google-oauth2 您的错误看起来像是您错过了初始化护照:

    app.use( passport.initialize());
    app.use( passport.session());
    

    【讨论】:

      猜你喜欢
      • 2018-08-25
      • 2021-08-25
      • 1970-01-01
      • 2013-05-22
      • 2019-12-26
      • 1970-01-01
      • 2019-03-02
      • 2019-01-26
      • 2018-12-02
      相关资源
      最近更新 更多