diff --git a/apps/backend/src/oauth/providers/github.tsx b/apps/backend/src/oauth/providers/github.tsx index 2884c0ae0..0c65a7f73 100644 --- a/apps/backend/src/oauth/providers/github.tsx +++ b/apps/backend/src/oauth/providers/github.tsx @@ -30,16 +30,37 @@ export class GithubProvider extends OAuthBaseProvider { } async postProcessUserInfo(tokenSet: TokenSet): Promise { - const rawUserInfo = await this.oauthClient.userinfo(tokenSet.accessToken); - - const emails = await fetch("https://api.github.com/user/emails", { + const rawUserInfoRes = await fetch("https://api.github.com/user", { headers: { Authorization: `token ${tokenSet.accessToken}`, }, - }).then((res) => res.json()); - if (!emails.find) { - throw new StackAssertionError("Error fetching user emails from github", { + }); + if (!rawUserInfoRes.ok) { + throw new StackAssertionError("Error fetching user info from GitHub provider: Status code " + rawUserInfoRes.status, { + rawUserInfoRes, + hasAccessToken: !!tokenSet.accessToken, + hasRefreshToken: !!tokenSet.refreshToken, + accessTokenExpiredAt: tokenSet.accessTokenExpiredAt, + }); + } + const rawUserInfo = await rawUserInfoRes.json(); + + const emailsRes = await fetch("https://api.github.com/user/emails", { + headers: { + Authorization: `token ${tokenSet.accessToken}`, + }, + }); + if (!emailsRes.ok) { + throw new StackAssertionError("Error fetching user emails from GitHub: Status code " + emailsRes.status, { + emailsRes, + rawUserInfo, + }); + } + const emails = await emailsRes.json(); + if (!Array.isArray(emails)) { + throw new StackAssertionError("Error fetching user emails from GitHub: Invalid response", { emails, + emailsRes, rawUserInfo, }); }