oauth2-github
oauth2-github copied to clipboard
Need primary github email, not just first email in list from /user/emails
Howdy! Is this project still alive?
I have found a way to get the primary email, not just the first email, from the GitHub API. Some users have multiple email addresses configured, but the primary one might not always be the first one. The code below fixes this:
protected function fetchResourceOwnerDetails(AccessToken $token)
{
$response = parent::fetchResourceOwnerDetails($token);
if (empty($response['email'])) {
$url = $this->getResourceOwnerDetailsUrl($token) . '/emails';
$request = $this->getAuthenticatedRequest(self::METHOD_GET, $url, $token);
$responseEmail = $this->getParsedResponse($request);
// sort email addresses based on importance
usort($responseEmail, function ($a, $b) {
if($a['primary'] !== $b['primary']) return $a['primary'] ? -1 : 1;
if($a['verified'] !== $b['verified']) return $a['verified'] ? -1 : 1;
return 0;
});
$response['email'] = isset($responseEmail[0]['email']) ? $responseEmail[0]['email'] : null;
}
return $response;
}
Another solution would be to return the data mostly as-is, and to do the same for all the providers, not just GitHub:
- email address
- bool is_primary
- bool is_confirmed
- visibility (private/public)
and let the user do what he wants with all the addresses, or add this list of emails as an extra data field besides the primary email.