-
Notifications
You must be signed in to change notification settings - Fork 25
Retrieve github user email #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
One additional api call is required to retrieve the github user's email. The returned json i format is ```json [ {"email":"email ... address", ...}] ``` For simplicity it was done as a one-liner.
src/multi_auth/providers/github.cr
Outdated
@@ -58,6 +58,8 @@ class MultiAuth::Provider::Github < MultiAuth::Provider | |||
gh_user = GhUser.from_json(raw_json) | |||
gh_user.access_token = access_token | |||
gh_user.raw_json = raw_json | |||
raw_email_json = api.get("/user/emails").body | |||
gh_user.email = JSON.parse(raw_email_json)[0]["email"].to_s |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code will raise an exception if no emails a returned (e.g. authorize_uri scope does not have email). Please make code work when no email in the response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@msa7
Done. Kindly confirm.
@kefahi Do you have plans to apply the requested changes? This would save me an extra API call I'm manually making, as well 😄 |
Absorb exceptions if the requested user email is not accessible.
@stephendolan |
@stephendolan @kefahi I just realized that get-the-authenticated-user already return the email. Why needs additional requests? |
@msa7 It had something to do with this user setting: When that box was checked, I'd get no email in the default response and had to make a follow-up request, which did return the email, even if it was private. Here's the full method I wrote that jogged my memory: # If a user's email is set to private, we have to make a subsequent request
# and fetch the primary email.
private def fetch_email_from_github(user : GitHubOAuthUser)
headers = HTTP::Headers{
"Authorization" => "token #{user.access_token}",
"Accept" => "application/vnd.github.v3+json",
}
api_response = HTTP::Client.get "https://api.github.com/user/emails", headers: headers
if api_response.status == HTTP::Status::OK
github_emails = Array(GitHubEmail).from_json(api_response.body)
primary_github_email = github_emails.find(&.primary)
if primary_github_email
primary_github_email.email
else
raise "User retrieved from GitHub API had no primary email."
end
else
raise "Failed to retrieve emails for user from GitHub API."
end
end And the little helper class for the response: class GitHubEmail
include JSON::Serializable
property email : String
property primary : Bool
end |
In reviewing the code I'd previously written, @kefahi, I do wonder if it'd be better to search for the primary email explicitly rather than just pulling the first one from the response. I'm not sure that the primary email is always guaranteed to be first in the response. |
One additional api call is required to retrieve the github user's email.
The returned json i format is
For simplicity it was done as a one-liner.