簡體   English   中英

在 Nodejs OAuth2 上與谷歌注冊時出現異步等待錯誤

[英]async-await error signing up with google on Nodejs OAuth2

我一直在嘗試注冊 google,但我收到了 async-await 錯誤,我不知道錯誤來自哪里。 下面是代碼

// Signup/Login a google user -- 2
router.post('/api/googlelogin', async (req, res) => {
    try {
        const { tokenId } = req.body
        client.verifyIdToken({idToken: tokenId, audience: process.env.GOOGLE_CLIENT_ID}).then( async (response) => {
            const {email_verified, name, email, picture} = response.payload;
            if (email_verified) {
                const user = await User.findOne({email})
                if (user) {
                    const token = await user.generateAuthToken()
                    res.send({ user, token })
                } else {
                    let password = email+process.env.ADMIN_SECRET_KEY
                    let newUser = new User({ email, fullname: name, password, profilePhoto: picture, isEmailConfirmed: true })
                    await newUser.save()
                    const token = await newUser.generateAuthToken()
                    res.status(201).send({ newUser, token })
                }
            }
        })
    } catch (e) {
        res.status(500).send({"message": "Something went wrong"})
    }
})

未處理的拒絕錯誤數

(node:30736) UnhandledPromiseRejectionWarning: Error: Wrong number of 
segments in token: eyJhbGciOiJSUzI1NiIsImtpZCI6ImZlZDgwZmVjNTZkYjk5MjMzZDRiNGY2MGZiYWZkYmFlYjkxODZjNzMiLCJ0eXAiOiJKV1QifQ
    at OAuth2Client.verifySignedJwtWithCertsAsync (D:\odede\Documents\Nodejs\eventCenter\node_modules\google-auth-library\build\src\auth\oauth2client.js:530:19)
    at OAuth2Client.verifyIdTokenAsync (D:\odede\Documents\Nodejs\eventCenter\node_modules\google-auth-library\build\src\auth\oauth2client.js:394:34)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:30736) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:30736) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.      

拜托,任何答案都會有所幫助,謝謝

這可能是一個簡單的修復:

 router.post('/api/googlelogin', async (req, res) => {
  try {
      const { tokenId } = req.body
      client.verifyIdToken({idToken: tokenId, audience: process.env.GOOGLE_CLIENT_ID})
.then( async (response) => { //add async here, it is an async callback 
          const {email_verified, name, email, picture} = response.payload;
          if (email_verified) {
              const user = await User.findOne({email})
              if (user) {
                  const token = await user.generateAuthToken()
                  res.send({ user, token })
              } else {
                  let password = email+process.env.ADMIN_SECRET_KEY
                  let newUser = new User({ email, fullname: name, password, profilePhoto: picture, isEmailConfirmed: true })
                  await newUser.save()
                  const token = await newUser.generateAuthToken()
                  res.status(201).send({ newUser, token })
              }
          }
      }).catch(e => { // you need to catch the error for promise 
        console.error(`Error: ${e}`)
      })
  } catch (e) {
      res.status(500).send({"message": "Something went wrong"})
  }
})

暫無
暫無

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

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