3

我学习了一门课程,它使用护照、passport-google-oauth20、cookie-session 实现了用户身份验证,一切正常(登录、注销、会话处理)但是当我发送登录/注册请求时它不会问/提示 google 身份验证窗口输入凭据,它始终使用相同的帐户登录。

这是护照策略配置:

const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const mongoose = require('mongoose');
const keys = require('../config/keys');

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.googleClientID,
      clientSecret: keys.googleClientSecret,
      callbackURL: '/auth/google/callback',
      proxy: true,
      authorizationParams: {
        access_type: 'offline',
        approval_prompt: 'force'
      }
    },
    async (accessToken, refreshToken, profile, done) => {
      const existingUser = await User.findOne({ googleID: profile.id })
        if (existingUser) {
          // we already have a record with the given profile ID
          return done(null, existingUser);
        }
          // we don't have a user record with this ID, make a new record!
          const user = await new User({ googleID: profile.id, name: profile.displayName }).save()
          done(null, user);
    })
);
4

1 回答 1

5

添加prompt: 'select_account'到路由中的passport.authenticate()中间件。/auth/google

app.get('/auth/google', passport.authenticate('google', {
   scope: ['profile', 'email'],
   prompt: 'select_account'
});

访问此页面:https ://developers.google.com/identity/protocols/OpenIDConnect#scope-param

于 2018-03-23T17:27:09.137 回答