@eaccess/auth 0.1.19 → 0.1.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +18 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +18 -7
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -785,11 +785,12 @@ var BaseOAuthProvider = class {
|
|
|
785
785
|
}
|
|
786
786
|
return data.access_token;
|
|
787
787
|
}
|
|
788
|
-
async fetchUserFromAPI(accessToken, apiUrl) {
|
|
788
|
+
async fetchUserFromAPI(accessToken, apiUrl, headers = {}) {
|
|
789
789
|
const response = await fetch(apiUrl, {
|
|
790
790
|
headers: {
|
|
791
791
|
Authorization: `Bearer ${accessToken}`,
|
|
792
|
-
Accept: "application/json"
|
|
792
|
+
Accept: "application/json",
|
|
793
|
+
...headers
|
|
793
794
|
}
|
|
794
795
|
});
|
|
795
796
|
if (!response.ok) {
|
|
@@ -820,14 +821,24 @@ var GitHubProvider = class extends BaseOAuthProvider {
|
|
|
820
821
|
throw new Error("No authorization code provided");
|
|
821
822
|
}
|
|
822
823
|
const accessToken = await this.exchangeCodeForToken(code, "https://github.com/login/oauth/access_token");
|
|
823
|
-
const
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
824
|
+
const apiHeaders = {
|
|
825
|
+
Accept: "application/vnd.github+json",
|
|
826
|
+
"User-Agent": this.authConfig.githubUserAgent || "EasyAccess",
|
|
827
|
+
"X-GitHub-Api-Version": "2022-11-28"
|
|
828
|
+
};
|
|
829
|
+
const [user, emails] = await Promise.all([
|
|
830
|
+
this.fetchUserFromAPI(accessToken, "https://api.github.com/user", apiHeaders),
|
|
831
|
+
this.fetchUserFromAPI(accessToken, "https://api.github.com/user/emails", apiHeaders)
|
|
832
|
+
]);
|
|
833
|
+
const verifiedEmails = Array.isArray(emails) ? emails.filter((email) => email.verified) : [];
|
|
834
|
+
const primaryEmail = verifiedEmails.find((email) => email.primary)?.email;
|
|
835
|
+
const fallbackEmail = primaryEmail || verifiedEmails[0]?.email;
|
|
836
|
+
if (!fallbackEmail) {
|
|
837
|
+
throw new Error("No verified email found in GitHub account");
|
|
827
838
|
}
|
|
828
839
|
return {
|
|
829
840
|
id: user.id.toString(),
|
|
830
|
-
email:
|
|
841
|
+
email: fallbackEmail,
|
|
831
842
|
username: user.login,
|
|
832
843
|
name: user.name || user.login,
|
|
833
844
|
avatar: user.avatar_url
|