From e0c874a3babb981727b10d6ed843718d505e4926 Mon Sep 17 00:00:00 2001 From: Chris Griffing Date: Thu, 14 Nov 2024 17:00:54 -0800 Subject: [PATCH] fix: get email from private email endpoint --- playgrounds/app/src/routes/api/oauth.ts | 35 +++++++++++++++++-------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/playgrounds/app/src/routes/api/oauth.ts b/playgrounds/app/src/routes/api/oauth.ts index 55d8646..ff47be2 100644 --- a/playgrounds/app/src/routes/api/oauth.ts +++ b/playgrounds/app/src/routes/api/oauth.ts @@ -24,18 +24,31 @@ export async function POST(event: APIEvent) { const { access_token } = await githubResponse.json() - const githubUserResponse = await fetch('https://api.github.com/user', { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - Accept: 'application/json', - Authorization: `Bearer ${access_token}`, - }, - }) + const [githubUserResponse, githubEmailsResponse] = await Promise.all([ + fetch('https://api.github.com/user', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json', + Authorization: `Bearer ${access_token}`, + }, + }), + fetch('https://api.github.com/user/emails', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json', + Authorization: `Bearer ${access_token}`, + }, + }), + ]) - const githubUser = await githubUserResponse.json() + const [githubUser, githubEmails] = await Promise.all([ + githubUserResponse.json(), + githubEmailsResponse.json(), + ]) - console.log({ ...githubUser }) + const githubEmail = githubEmails.find((email: { primary: boolean }) => email.primary)?.email let user = await db.query.users.findFirst({ where: eq(usersTable.githubId, githubUser.id), @@ -45,7 +58,7 @@ export async function POST(event: APIEvent) { const createdAt = Date.now() const newUser = { id: customNanoid(), - email: githubUser.email, + email: githubUser.email || githubEmail, githubId: githubUser.id, githubUsername: githubUser.login, githubAvatarUrl: githubUser.avatar_url,