Skip to content

Commit

Permalink
fix: get email from private email endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cmgriffing committed Nov 15, 2024
1 parent 1d69bc5 commit e0c874a
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions playgrounds/app/src/routes/api/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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,
Expand Down

0 comments on commit e0c874a

Please sign in to comment.