Skip to content
This repository was archived by the owner on Jul 16, 2021. It is now read-only.

Commit a5cdec4

Browse files
authored
fix GitHub OAuth scope and data retrieval (#380)
- switch to limited user:email scope - correctly fetch email and user name from GitHub
1 parent d5beffe commit a5cdec4

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/api/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func GetOAuthConfig(host string) (*oauth2.Config, error) {
100100
ClientSecret: clientSecret,
101101
Endpoint: oauth2Github.Endpoint,
102102
RedirectURL: "http://" + host + "/api/auth/github/callback",
103-
Scopes: []string{"repo"},
103+
Scopes: []string{"user:email"},
104104
}, nil
105105
}
106106

src/api/handlers/auth.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,29 @@ func (a *AuthHandlers) GithubCallback(w http.ResponseWriter, r *http.Request) {
9292
errorResponse(w, http.StatusInternalServerError, "unable to retrieve user")
9393
return
9494
}
95+
emails, _, err := client.Users.ListEmails(oauth2.NoContext, nil)
96+
if err != nil {
97+
errorResponse(w, http.StatusInternalServerError, "unable to retrieve user email")
98+
return
99+
}
100+
101+
var userEmail string
102+
for _, email := range emails {
103+
if email.GetPrimary() {
104+
userEmail = email.GetEmail()
105+
break
106+
}
107+
}
95108

96109
db, closer := a.dbSession.DB()
97110
defer closer()
98-
if err := models.CreateUser(db, &models.User{Name: *user.Name, Email: *user.Email}); err != nil {
111+
if err := models.CreateUser(db, &models.User{Name: user.GetName(), Email: userEmail}); err != nil {
99112
errorResponse(w, http.StatusInternalServerError, "unable to save user")
100113
return
101114
}
102115

103116
// Fetch from DB to get ID
104-
u, err := models.GetUserByEmail(db, *user.Email)
117+
u, err := models.GetUserByEmail(db, userEmail)
105118

106119
claims := models.UserClaims{
107120
User: u,
@@ -125,7 +138,7 @@ func (a *AuthHandlers) GithubCallback(w http.ResponseWriter, r *http.Request) {
125138
http.SetCookie(w, &jwtCookie)
126139
http.SetCookie(w, &claimsCookie)
127140

128-
http.Redirect(w, r, r.Referer(), http.StatusFound)
141+
http.Redirect(w, r, "/", http.StatusFound)
129142
}
130143

131144
// Logout clears the JWT token cookie

0 commit comments

Comments
 (0)