From 5824eae0a6796259ce118b873f3c48df02626139 Mon Sep 17 00:00:00 2001 From: Konstantin Wohlwend Date: Mon, 17 Feb 2025 11:46:14 -0800 Subject: [PATCH] Fix STACK-BACKEND-6V --- apps/backend/src/oauth/providers/github.tsx | 33 +++++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) 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, }); }