Fix STACK-BACKEND-6V

This commit is contained in:
Konstantin Wohlwend 2025-02-17 11:46:14 -08:00
parent 21454eb23a
commit 5824eae0a6

View File

@ -30,16 +30,37 @@ export class GithubProvider extends OAuthBaseProvider {
}
async postProcessUserInfo(tokenSet: TokenSet): Promise<OAuthUserInfo> {
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,
});
}