【问题标题】:Google OAuth 2.0 email missing from profile配置文件中缺少 Google OAuth 2.0 电子邮件
【发布时间】:2023-01-11 18:04:17
【问题描述】:

我正在尝试通过 Passport 设置 Google OAuth 2.0。我正在使用 express 开发 node.js。 Node.js:v18.12.1

当尚未创建用户时,我尝试根据 Google 提供的信息创建它。 但是,由于某种原因,电子邮件丢失了。

我在 OAuth 2.0 上使用的范围:

问题的代码摘录:


passport.use(new googleAuth.Strategy({
        clientID: process.env.GOOGLE_CLIENT_ID!,
        clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
        callbackURL: "http://localhost:3000/auth/google/secrets",
        scope: ["email","profile"]
    },
    (accessToken: string, refreshToken: string, profile: googleAuth.Profile, cb: VerifyCallback) => {
        User.findOne({googleId: profile.id}, (err: CallbackError, user: PassportLocalModel<IUser>) => {
            if(err){
                return cb(err);
            }else if(user){
                return cb(err, user);
            }else{
                const user = new User({
                    email: profile.emails![0].value,
                    googleId: profile.id,
                });

                user.save((err: CallbackError) => {
                    if(err){
                        console.log(err);
                        cb(err);
                    }
                })
            }
        })
       
    }
));

Profile.emails 和 _json.email 都未定义(cf:https://developers.google.com/identity/openid-connect/openid-connect#an-id-tokens-payload

知道为什么吗?

不要犹豫,询问是否需要更多信息。

谢谢

编辑 :

_json 的内容(隐藏的真实内容):

 _json: {
    sub: <somestring>,
    name: <some name>,
    given_name: <some name>,
    family_name: <some name>,
    picture: <some url>,
    locale: 'en-GB'
  }

【问题讨论】:

  • 当你想创建用户时,你试过这样吗:email: profile._json.email
  • @ShueiYang 正如你在我的编辑中看到的,属性“email”不在_json中

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


【解决方案1】:

我找到了解决问题的方法。

结果我在身份验证期间覆盖了范围选项。

app.get("/auth/google", 
        passport.authenticate("google", {scope: ["profile"]})
);

通过简单地删除选项,问题就解决了。

app.get("/auth/google", 
        passport.authenticate("google")
);

解决方案

passport.use(new googleAuth.Strategy({
        clientID: process.env.GOOGLE_CLIENT_ID!,
        clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
        callbackURL: "http://localhost:3000/auth/google/secrets",
        scope: ["profile", "email"]
    },
    (accessToken: string, refreshToken: string, profile: googleAuth.Profile, cb: VerifyCallback) => {
        User.findOne({googleId: profile.id}, (err: CallbackError, user: PassportLocalModel<IUser>) => {
            if(err){
                return cb(err);
            }else if(user){
                return cb(err, user);
            }else{
                const user = new User({
                    email: profile.emails![0].value,
                    googleId: profile.id,
                });

                user.save((err: CallbackError) => {
                    if(err){
                        console.log(err);
                        cb(err);
                    }
                })
            }
        })
       
    }
));

app.get("/auth/google", 
        passport.authenticate("google")
    );


【讨论】:

    【解决方案2】:

    您还可以删除 google 策略上的选项并像这样在您的授权路线上使用

     passport.use(new googleAuth.Strategy({
              clientID: process.env.GOOGLE_CLIENT_ID!,
              clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
              callbackURL: "http://localhost:3000/auth/google/secrets",
          },
          (accessToken: string, refreshToken: string, profile: googleAuth.Profile, cb: VerifyCallback) => {
               //...
          }
     ));
    
        app.get("/auth/google", 
            passport.authenticate("google", {scope: ["email", "profile"]})
    );
    

    正如您在他们的文档中看到的那样:https://www.passportjs.org/packages/passport-google-oauth20/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 2012-01-03
      • 1970-01-01
      • 1970-01-01
      • 2015-04-03
      • 2018-05-21
      • 2012-09-18
      相关资源
      最近更新 更多