Google 第三方登入

NPM

圖示

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

OAuth 2.0 的授權

簡單來說其實就是獲取令牌(token)的過程。OAuth 協定定義了四種獲取令牌的授權方式(authorization grant)如下:

  • 授權碼模式(authorization-code)
  • 隱藏模式(implicit)
  • 密碼模式(password)
  • 客戶端憑證模式(client credentials)

延伸資訊

前後端分離後端寫法

前端

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <a href="https://accounts.google.com/o/oauth2/v2/auth?client_id=771760535711-6it451lk1m6ah82gk5rps86p7qsopkt6.apps.googleusercontent.com&redirect_uri=http://localhost:5500/googlecallback.html&response_type=code&scope=email%20profile&access_type=offline">Google 登入</a>

</body>
</html>

後端

router.post('/googleClient/callback', passport.authenticate('google', { session: false }), handleErrorAsync(async(req,res,next)=> {
  const user = await User.findById(req.user.id);
  generateSendJWT(user,200,res)
}))