New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(providers): rewrite github provider in typescript #4908
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import type { OAuthConfig, OAuthUserConfig } from "." | ||
|
||
/** | ||
* Source https://docs.github.com/en/rest/users/users#get-the-authenticated-user | ||
*/ | ||
export interface GithubProfile extends Record<string, any> { | ||
login: string | ||
id: number | ||
node_id: string | ||
avatar_url: string | ||
gravatar_id: string | null | ||
url: string | ||
html_url: string | ||
followers_url: string | ||
following_url: string | ||
gists_url: string | ||
starred_url: string | ||
subscriptions_url: string | ||
organizations_url: string | ||
repos_url: string | ||
events_url: string | ||
received_events_url: string | ||
type: string | ||
site_admin: boolean | ||
name: string | null | ||
company: string | null | ||
blog: string | null | ||
location: string | null | ||
email: string | null | ||
hireable: boolean | null | ||
bio: string | null | ||
twitter_username?: string | null | ||
public_repos: number | ||
public_gists: number | ||
followers: number | ||
following: number | ||
created_at: string | ||
updated_at: string | ||
private_gists?: number | ||
total_private_repos?: number | ||
owned_private_repos?: number | ||
disk_usage?: number | ||
suspended_at?: string | null | ||
collaborators?: number | ||
two_factor_authentication: boolean | ||
plan?: { | ||
collaborators: number | ||
name: string | ||
space: number | ||
private_repos: number | ||
} | ||
} | ||
|
||
export interface GithubEmail extends Record<string, any> { | ||
email: string | ||
primary: boolean | ||
verified: boolean | ||
visibility: string | null | ||
} | ||
|
||
export default function Github<P extends GithubProfile>( | ||
options: OAuthUserConfig<P> | ||
): OAuthConfig<P> { | ||
return { | ||
id: "github", | ||
name: "GitHub", | ||
type: "oauth", | ||
authorization: { | ||
url: "https://github.com/login/oauth/authorize", | ||
params: { scope: "read:user+user:email" }, | ||
}, | ||
token: "https://github.com/login/oauth/access_token", | ||
userinfo: { | ||
url: "https://api.github.com/user", | ||
async request({ client, tokens }) { | ||
// Get base profile | ||
const profile = await client.userinfo(tokens.access_token!) | ||
|
||
// If user has email hidden, get their primary email from the GitHub API | ||
if (!profile.email) { | ||
const emails: GithubEmail[] = await ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added to the types to the email request too. |
||
await fetch("https://api.github.com/user/emails", { | ||
headers: { Authorization: `token ${tokens.access_token}` }, | ||
}) | ||
).json() | ||
|
||
if (emails?.length > 0) { | ||
// Get primary email | ||
profile.email = emails.find((email) => email.primary)?.email | ||
// And if for some reason it doesn't exist, just use the first | ||
if (!profile.email) profile.email = emails[0].email | ||
} | ||
} | ||
Comment on lines
+80
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this is still needed. But probably somebody added it for a reason. |
||
|
||
return profile | ||
}, | ||
}, | ||
profile(profile) { | ||
return { | ||
id: profile.id.toString(), | ||
name: profile.name ?? profile.login, | ||
email: profile.email, | ||
image: profile.avatar_url, | ||
} | ||
}, | ||
options, | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/nextauthjs/next-auth/runs/7305230206?check_suite_focus=true#step:7:22
This throws as well. I believe it should always be
string
though, so maybe a// @ts-expect-error
is forgivable here. If not, we can change it to throw an error in the future.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the same behavior of https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/providers/facebook.ts