简体   繁体   English

使用Passport-google-oauth20时自动登录

[英]Auto login while using passport-google-oauth20

I followed a course and it implemented user authentication using passport, passport-google-oauth20, cookie-session and it all works fine (login, logout, session handling) but when i send a request for a Log in/Sign Up it doesnt ask/prompt the google authentication window to enter the credentials, it always logs in with the same account. 我遵循了一个课程,它使用通行证,passport-google-oauth20,cookie-session实现了用户身份验证,并且一切正常(登录,注销,会话处理),但是当我发送登录/注册请求时,它不会询问/提示Google身份验证窗口输入凭据,它始终使用同一帐户登录。

Here is the passport-strategy configuration: 这是护照策略配置:

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

Add prompt: 'select_account' to the passport.authenticate() middleware in your /auth/google route. /auth/google路由中,向prompt: 'select_account' passport.authenticate()中间件添加prompt: 'select_account'

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google OAuth Node.js使用通行证(passport-google-oauth20)处理错误 - Google OAuth Nodejs handle error using passport, passport-google-oauth20 如何在同一个应用程序中两次使用passport-google-oauth20 - how to use passport-google-oauth20 twice in the same app 使用passport-google-oauth20,尝试为图片URL创建项目mongo - Using passport-google-oauth20, trying to create item mongo for picture URL 护照-谷歌-oauth2 vs护照-谷歌-oauth20软件包 - passport-google-oauth2 vs passport-google-oauth20 packages 使用passport-google-oauth20 InternalOAuthError获取错误:无法获取用户配置文件并且在将标头发送到客户端后无法设置标头 - Getting erros using passport-google-oauth20 InternalOAuthError: Failed to fetch user profile and Cannot set headers after they are sent to the client 如何使用 passport-google-oauth20 获取用户 email - How can I get user email with passport-google-oauth20 Google oauth20的护照,未使用的中间件,passport.initialize() - Passport for Google oauth20, middleware not in use, passport.initialize() ?nodejs中护照google-oauth20中代理的用途是什么? - ?What is the use of a proxy in passport google-oauth20 in nodejs? 使用护照js登录谷歌 - Login with google using passport js 如何将Json Web Token(JWT)和Google oauth结合使用passwordjs,passport-google-oauth和node来创建社交登录系统? - How to combine Json Web Token(JWT) and google oauth using passportjs, passport-google-oauth and node to create social login system?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM