1
1
import type { OAuthConfig , OAuthUserConfig } from "."
2
2
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 */
6
4
export interface GithubProfile extends Record < string , any > {
7
5
login : string
8
6
id : number
@@ -55,7 +53,7 @@ export interface GithubEmail extends Record<string, any> {
55
53
email : string
56
54
primary : boolean
57
55
verified : boolean
58
- visibility : string | null
56
+ visibility : "public" | "private"
59
57
}
60
58
61
59
export default function Github < P extends GithubProfile > (
@@ -67,29 +65,25 @@ export default function Github<P extends GithubProfile>(
67
65
type : "oauth" ,
68
66
authorization : {
69
67
url : "https://github.com/login/oauth/authorize" ,
70
- params : { scope : "read:user+ user:email" } ,
68
+ params : { scope : "read:user user:email" } ,
71
69
} ,
72
70
token : "https://github.com/login/oauth/access_token" ,
73
71
userinfo : {
74
72
url : "https://api.github.com/user" ,
75
73
async request ( { client, tokens } ) {
76
- // Get base profile
77
74
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
78
75
const profile = await client . userinfo ( tokens . access_token ! )
79
76
80
- // If user has email hidden, get their primary email from the GitHub API
81
77
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
+ } )
87
83
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
93
87
}
94
88
}
95
89
0 commit comments