node.js - 使用 passport-google-oauth20 时自动登录

标签 node.js cookie-session passport-google-oauth2

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

这是护照策略配置:

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);
    })
);

最佳答案

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

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

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

关于node.js - 使用 passport-google-oauth20 时自动登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47464988/

相关文章:

node.js - Sails.js - PATH 变量 - 无法识别 sails 命令

node.js - 内部 OAuth 错误 : Failed to fetch user profile NodeJs+passport-google-oAuth2

node.js - Passport for Facebook 和 Google 中所有可能的范围选项

node.js - cookie-session中间件在expressjs中如何工作?

node.js - 登录成功后如何发送JWT到前端服务器存储到localStorage?

node.js - 通过管道将二进制文件从 stdout 传输到 http 响应

node.js - Exercism 练习 : jasmine-node . - 找不到命令

javascript - 使用变量时如何防止正则表达式测试切换?

node.js - 如何在 Node Express.js 中使用 cookie-session