Skip to content

Commit 84071d8

Browse files
committed
fix profile and email fetching
1 parent 95b8a0d commit 84071d8

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

app/utils/providers/github.server.ts

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { redirect } from 'react-router'
44
import { GitHubStrategy } from 'remix-auth-github'
55
import { z } from 'zod'
66
import { cache, cachified } from '../cache.server.ts'
7-
import { connectionSessionStorage } from '../connections.server.ts'
87
import { type Timings } from '../timing.server.ts'
98
import { MOCK_CODE_GITHUB_HEADER, MOCK_CODE_GITHUB } from './constants.ts'
109
import { type AuthProvider } from './provider.ts'
@@ -25,6 +24,20 @@ const shouldMock =
2524
process.env.GITHUB_CLIENT_ID?.startsWith('MOCK_') ||
2625
process.env.NODE_ENV === 'test'
2726

27+
type GitHubEmailsResponse = {
28+
email: string
29+
verified: boolean
30+
primary: boolean
31+
visibility: string | null
32+
}[]
33+
34+
type GitHubUserResponse = {
35+
login: string
36+
id: string
37+
name: string | undefined
38+
avatar_url: string | undefined
39+
}
40+
2841
export class GitHubProvider implements AuthProvider {
2942
getAuthStrategy() {
3043
return new GitHubStrategy(
@@ -34,26 +47,39 @@ export class GitHubProvider implements AuthProvider {
3447
redirectURI: '/auth/github/callback',
3548
},
3649
async ({ tokens }) => {
37-
const response = await fetch('https://api.github.com/user', {
50+
// we need to fetch the user and the emails separately, this is a change in remix-auth-github
51+
// from the previous version that supported fetching both in one call
52+
const userResponse = await fetch('https://api.github.com/user', {
3853
headers: {
3954
Accept: 'application/vnd.github+json',
4055
Authorization: `Bearer ${tokens.accessToken()}`,
4156
'X-GitHub-Api-Version': '2022-11-28',
4257
},
4358
})
44-
const profile = (await response.json()) as any
45-
const email = profile.emails[0]?.trim().toLowerCase()
59+
const user = (await userResponse.json()) as GitHubUserResponse
60+
61+
const emailsResponse = await fetch(
62+
'https://api.github.com/user/emails',
63+
{
64+
headers: {
65+
Accept: 'application/vnd.github+json',
66+
Authorization: `Bearer ${tokens.accessToken()}`,
67+
'X-GitHub-Api-Version': '2022-11-28',
68+
},
69+
},
70+
)
71+
const emails = (await emailsResponse.json()) as GitHubEmailsResponse
72+
const email = emails.find((e) => e.primary)?.email
4673
if (!email) {
4774
throw new Error('Email not found')
4875
}
49-
// const username = profile.displayName
50-
// const imageUrl = profile.photos[0]?.value
76+
5177
const returnValue = {
52-
id: profile.id,
78+
id: user.id,
5379
email,
54-
// username,
55-
// name: profile.name,
56-
// imageUrl,
80+
name: user.name,
81+
username: user.login,
82+
imageUrl: user.avatar_url,
5783
}
5884
return returnValue
5985
},

0 commit comments

Comments
 (0)