Skip to content

Commit 05bf014

Browse files
feat(providers): support self-hosted github (#9271)
* feat: add support for self-hosted github * fix JSDoc comments as suggested * Apply suggestions from code review * Update github.ts --------- Co-authored-by: Balázs Orbán <[email protected]>
1 parent 1ce4158 commit 05bf014

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

packages/core/src/providers/github.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,30 @@ export interface GitHubProfile {
120120
* :::
121121
*/
122122
export default function GitHub(
123-
config: OAuthUserConfig<GitHubProfile>
123+
config: OAuthUserConfig<GitHubProfile> & {
124+
/** Configuration for usage with [GitHub Enterprise Server](https://docs.github.com/en/enterprise-server/get-started). */
125+
enterprise?: {
126+
/** The base URL of your GitHub Enterprise Server instance. */
127+
baseUrl?: string
128+
}
129+
}
124130
): OAuthConfig<GitHubProfile> {
131+
const baseUrl = config.enterprise?.baseUrl ?? "https://github.com"
132+
const apiBaseUrl = config.enterprise?.baseUrl
133+
? `${config.enterprise?.baseUrl}/api/v3`
134+
: "https://api.github.com"
135+
125136
return {
126137
id: "github",
127138
name: "GitHub",
128139
type: "oauth",
129140
authorization: {
130-
url: "https://github.com/login/oauth/authorize",
141+
url: `${baseUrl}/login/oauth/authorize`,
131142
params: { scope: "read:user user:email" },
132143
},
133-
token: "https://github.com/login/oauth/access_token",
144+
token: `${baseUrl}/login/oauth/access_token`,
134145
userinfo: {
135-
url: "https://api.github.com/user",
146+
url: `${apiBaseUrl}/user`,
136147
async request({ tokens, provider }) {
137148
const profile = await fetch(provider.userinfo?.url as URL, {
138149
headers: {
@@ -144,7 +155,7 @@ export default function GitHub(
144155
if (!profile.email) {
145156
// If the user does not have a public email, get another via the GitHub API
146157
// See https://docs.github.com/en/rest/users/emails#list-public-email-addresses-for-the-authenticated-user
147-
const res = await fetch("https://api.github.com/user/emails", {
158+
const res = await fetch(`${apiBaseUrl}/user/emails`, {
148159
headers: {
149160
Authorization: `Bearer ${tokens.access_token}`,
150161
"User-Agent": "authjs",

0 commit comments

Comments
 (0)