Skip to content
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

use user's primary email if public email is null #1543

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion fastn-core/src/auth/github/apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ pub async fn is_user_sponsored(
}
}

// TODO: It can be stored in the request cookies
pub async fn user_details(access_token: &str) -> fastn_core::Result<GhUserDetails> {
// API Docs: https://docs.github.com/en/rest/users/users#get-the-authenticated-user
// TODO: Handle paginated response
Expand All @@ -147,3 +146,19 @@ pub async fn user_details(access_token: &str) -> fastn_core::Result<GhUserDetail

Ok(user_obj)
}

#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct GhEmail {
pub email: String,
pub verified: bool,
pub primary: bool,
pub visibility: String,
}

pub async fn user_emails(access_token: &str) -> fastn_core::Result<Vec<GhEmail>> {
// API Docs: https://docs.github.com/en/rest/users/emails?apiVersion=2022-11-28#list-email-addresses-for-the-authenticated-user
let user_obj: Vec<GhEmail> =
fastn_core::http::get_api("https://api.github.com/user/emails", access_token).await?;

Ok(user_obj)
}
13 changes: 12 additions & 1 deletion fastn-core/src/auth/github/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,18 @@ pub async fn callback(
Err(e) => return Ok(fastn_core::server_error!("{}", e.to_string())),
};

let gh_user = fastn_core::auth::github::apis::user_details(access_token.as_str()).await?;
let mut gh_user = fastn_core::auth::github::apis::user_details(access_token.as_str()).await?;

if gh_user.email.is_none() {
// pick primary email
let emails = fastn_core::auth::github::apis::user_emails(access_token.as_str()).await?;
let primary = emails
.into_iter()
.find(|e| e.primary)
.expect("primary email must exist for a github account");

gh_user.email = Some(primary.email);
}

let ud = UserDetail {
user: gh_user,
Expand Down