簡體   English   中英

配置文件中缺少 Google OAuth 2.0 email

[英]Google OAuth 2.0 email missing from profile

我正在嘗試通過 Passport 設置 Google OAuth 2.0。 我正在使用 express 開發一個 node.js。 Node.js:v18.12.1

當尚未創建用戶時,我嘗試根據 Google 提供的信息創建它。 但是,由於某種原因缺少 email。

Scope 我在 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 未定義(參見: 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'
  }

我找到了解決問題的辦法。

結果我在身份驗證期間覆蓋了 scope 選項。

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


您還可以刪除 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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM