Use email api call

This commit is contained in:
Jelle Besseling 2020-12-30 21:32:00 +01:00
parent bbd4891133
commit e88355b3d4
No known key found for this signature in database
GPG key ID: 9712452E8BE3372E

View file

@ -181,11 +181,23 @@ sub github_login :Path('/github-login') Args(0) {
my $data = decode_json($response->decoded_content) or die; my $data = decode_json($response->decoded_content) or die;
my $access_token = $data->{access_token} // die "No access_token in response from GitHub."; my $access_token = $data->{access_token} // die "No access_token in response from GitHub.";
$response = $ua->get('https://api.github.com/user', Authorization => "token $access_token"); $response = $ua->get('https://api.github.com/user/emails', Accept => 'application/vnd.github.v3+json', Authorization => "token $access_token");
error($c, "Did not get a response from GitHub for user info.") unless $response->is_success; error($c, "Did not get a response from GitHub for email info.") unless $response->is_success;
$data = decode_json($response->decoded_content) or die; $data = decode_json($response->decoded_content) or die;
doEmailLogin($self, $c, "github", $data->{email}, $data->{name} // undef); my $email;
foreach my $eml (@{$data}) {
$email = $eml->{email} if $eml->{verified} && $eml->{primary};
print STDERR "$eml->{email}\n";
}
print STDERR "$email\n";
$response = $ua->get('https://api.github.com/user', Authorization => "token $access_token");
error($c, "Did not get a response from GitHub for user info.") unless $response->is_success;
$data = decode_json($response->decoded_content) or die;
doEmailLogin($self, $c, "github", $email, $data->{name} // undef);
$c->res->redirect($c->uri_for($c->res->cookies->{'after_github'})); $c->res->redirect($c->uri_for($c->res->cookies->{'after_github'}));
} }