Skip to content

Commit bb664a2

Browse files
authoredJul 15, 2022
fix(providers): typo in GitHub provider scope (#4938)
1 parent a14fbea commit bb664a2

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed
 

‎packages/next-auth/src/providers/github.ts

+11-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import type { OAuthConfig, OAuthUserConfig } from "."
22

3-
/**
4-
* Source https://docs.github.com/en/rest/users/users#get-the-authenticated-user
5-
*/
3+
/** @see https://docs.github.com/en/rest/users/users#get-the-authenticated-user */
64
export interface GithubProfile extends Record<string, any> {
75
login: string
86
id: number
@@ -55,7 +53,7 @@ export interface GithubEmail extends Record<string, any> {
5553
email: string
5654
primary: boolean
5755
verified: boolean
58-
visibility: string | null
56+
visibility: "public" | "private"
5957
}
6058

6159
export default function Github<P extends GithubProfile>(
@@ -67,29 +65,25 @@ export default function Github<P extends GithubProfile>(
6765
type: "oauth",
6866
authorization: {
6967
url: "https://github.com/login/oauth/authorize",
70-
params: { scope: "read:user+user:email" },
68+
params: { scope: "read:user user:email" },
7169
},
7270
token: "https://github.com/login/oauth/access_token",
7371
userinfo: {
7472
url: "https://api.github.com/user",
7573
async request({ client, tokens }) {
76-
// Get base profile
7774
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
7875
const profile = await client.userinfo(tokens.access_token!)
7976

80-
// If user has email hidden, get their primary email from the GitHub API
8177
if (!profile.email) {
82-
const emails: GithubEmail[] = await (
83-
await fetch("https://api.github.com/user/emails", {
84-
headers: { Authorization: `token ${tokens.access_token}` },
85-
})
86-
).json()
78+
// If the user does not have a public email, get another via the GitHub API
79+
// See https://docs.github.com/en/rest/users/emails#list-public-email-addresses-for-the-authenticated-user
80+
const res = await fetch("https://api.github.com/user/emails", {
81+
headers: { Authorization: `token ${tokens.access_token}` },
82+
})
8783

88-
if (emails?.length > 0) {
89-
// Get primary email
90-
profile.email = emails.find((email) => email.primary)?.email
91-
// And if for some reason it doesn't exist, just use the first
92-
if (!profile.email) profile.email = emails[0].email
84+
if (res.ok) {
85+
const emails: GithubEmail[] = await res.json()
86+
profile.email = (emails.find((e) => e.primary) ?? emails[0]).email
9387
}
9488
}
9589

0 commit comments

Comments
 (0)
Please sign in to comment.