Could not get name from Google with goth
lovelyrrg51 opened this issue · 2 comments
lovelyrrg51 commented
Hey Guys.
With google oauth on goth, could not get name.
Please let me know how to handle it.
Thanks.
- Init Store & Goth Provider
maxAge := 86400 * 30 // 30 days
isProd := false // Set to true when serving over https
store := sessions.NewCookieStore([]byte(config.Cfg.GoogleClientSecret))
store.MaxAge(maxAge)
store.Options.Path = "/"
store.Options.HttpOnly = true // HttpOnly should always be enabled
store.Options.Secure = isProd
gothic.Store = store
return AuthController{
googleProvider: google.New(config.Cfg.GoogleClientID, config.Cfg.GoogleClientSecret, config.Cfg.GoogleCallbackURL),
}
- Google Provider
// Update Goth Provider
goth.UseProviders(aCtrl.googleProvider)
// Update Request
q := c.Request.URL.Query()
q.Add("provider", "google")
c.Request.URL.RawQuery = q.Encode()
// Return Google Handler
gothic.BeginAuthHandler(c.Writer, c.Request)
- Google Provider Callback
// Update Goth Provider
goth.UseProviders(aCtrl.googleProvider)
// Update Request
q := c.Request.URL.Query()
q.Add("provider", "google")
c.Request.URL.RawQuery = q.Encode()
// Get User Info From Google Provider
user, err := gothic.CompleteUserAuth(c.Writer, c.Request)
if err != nil {
utils.WriteResponseError(c, common.NewBadRequestError(err.Error()))
}
fmt.Println("===== User Info: =====", user)
fmt.Println("====================== User Email: ", user.Email)
fmt.Println("====================== User Name: ", user.Name)
fmt.Println("====================== User IDToken: ", user.IDToken)
fmt.Println("====================== User AccessToken: ", user.AccessToken)
- Output
===== User Info: ===== {map[email:dragonwarrior920619@gmail.com id:110134761728427896598 picture:https://lh3.googleusercontent.com/a/default-user=s96-c verified_email:true] google dragonwarrior920619@gmail.com 110134761728427896598 https://lh3.googleusercontent.com/a/default-user=s96-c ya29.a0AWY7CkkiYDJENCioWNhKwwtuauYKBmBrC-y8Zx-tT5w5RyjeKOPT1DUrsKaj9kwxiCw72UsW2lAvqZDD9vSoSv5jXIBeTxTJYLBg774RKQ0LdorWR8nxDiAfFbFl8t-0165 2023-06-19 03:17:40.763992897 -0600 MDT m=+3609.651427661 4OTY1OTgiLCJlbWFpbCI6ImRyYWdvbndhcnJpb3I5MjA2MTlAZ21haWwuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImF0X2hhc2giOiJkQXFQaFcyNkVkOFNqN3k1VTdpX2NnIiwiaWF0IjoxNjg3MTYyNjYxLCJleHAiOjE2ODcxNjYyNjF9.eG9cltTYZiVsUImEA456WTkTN4jGDjxQjqtJGfu0Uv-VhMbyD3zxcxtlVtW8wXBBHfKCTcQ1zY4r407_zvVawAIkbIqM_CgNu1jV0eiaXJ0Zlr8Cy1AqFlDWmoh2omcGSqysQAYODlrHKb6l2onQdpE9upro-xwNi9nUw4pEfHzqj-bTr4ohWMcMvB-hJ01HqCRYd62thebIdPUAIQpMeig1HBaBPhsqHFQ-Z7javRnZvXJRaegUXHSEYQngwz5jHrN7Jflf9nMQmcMYH7_r51oB0q9w}
====================== User Email: dragonwarrior920619@gmail.com
====================== User Name:
====================== User IDToken: hhc2giOiJkQXFQaFcyNWTkTN4jGDjxQjqtJGfu0Uv-VhMbyD3zxcxtlVtW8wQsVLnBFWC3LybLGWzSLbSs5E3d-EOvxY_a_2iF4hBSXkSSIMyNVz7GLdn-XBBHfKCTcQ1zY4r407_zvVawAIkbIqM_CgNu1jV0eiaXJ0Zlr8Cy1AqFlDWmoh2omcGSqysQAYODlrHKb6l2onQdpE9upro-xwNi9nUw4pEfHzqj-bTr4ohWMcMvB-hJ01HqCRYd62thebIdPUAIQpMeig1HBaBPhsqHFQ-Z7javRnZvXJRaegUXHSEYQngwz5jHrN7Jflf9nMQmcMYH7_r51oB0q9w
====================== User AccessToken: oSv5jXIBeTxTJYLBg774RKQ0LdorWR8nxDiAfFbFl8t-j3Twy6i5ALVzGUSiiq8BEGPqGydlERwaCgYKAc4SARESFQG1tDrpcoJ3FUT8QYTSLuO0jTsVhg0165
[GIN] 2023/06/19 - 02:17:42 | 200 | 1.043941956s | ::1 | GET "/v1/auth/google/redirect?state=WinyuIa9p8JidoAeZjvRFnCYNYgq-Uj5EB3bYlVLaPOm31eI2Mkf2tpH8e9M7Jfg1nHhNyeddgNHc9wLTO2e0Q%3D%3D&code=4%2F0AbUR2VN3-lCVKbsZYmNM-CZbg5cymbXWuH2wVv4ddc1lr9Ynocmv_7dJ_jpAo7wB2NVLJQ&scope=email+openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&authuser=0&prompt=none"
mortenson commented
@lovelyrrg51 Good timing, I just ran into this too. You'll need to declare the scopes you need if you want more than the user's email, ex:
google.New(clientId, clientSecret, callbackUrl, "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile")
With https://www.googleapis.com/auth/userinfo.profile
added you should get the user's name.
lovelyrrg51 commented
It's working. Thanks, @mortenson