Skip to content

Commit 745751e

Browse files
Tobix99himself65
andauthored
feat(providers): support custom baseURL for Gitlab (#13260)
Co-authored-by: Alex Yang <[email protected]>
1 parent 03cfd79 commit 745751e

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

docs/pages/getting-started/providers/gitlab.mdx

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,14 @@ import NextAuth from "next-auth"
5454
import GitLab from "next-auth/providers/gitlab"
5555

5656
export const { handlers, auth, signIn, signOut } = NextAuth({
57-
providers: [GitLab],
57+
providers: [
58+
// Default (gitlab.com)
59+
GitLab,
60+
// Self-hosted example
61+
GitLab({
62+
baseUrl: "https://gitlab.example.com",
63+
}),
64+
],
5865
})
5966
```
6067

@@ -67,7 +74,14 @@ import GitLab from "@auth/qwik/providers/gitlab"
6774

6875
export const { onRequest, useSession, useSignIn, useSignOut } = QwikAuth$(
6976
() => ({
70-
providers: [GitLab],
77+
providers: [
78+
GitLab,
79+
GitLab({
80+
instance: {
81+
baseUrl: "https://gitlab.example.com"
82+
}
83+
})
84+
],
7185
})
7286
)
7387
```
@@ -80,7 +94,12 @@ import { SvelteKitAuth } from "@auth/sveltekit"
8094
import GitLab from "@auth/sveltekit/providers/gitlab"
8195

8296
export const { handle, signIn, signOut } = SvelteKitAuth({
83-
providers: [GitLab],
97+
providers: [
98+
GitLab,
99+
GitLab({
100+
baseUrl: "https://gitlab.example.com",
101+
}),
102+
],
84103
})
85104
```
86105

@@ -91,7 +110,17 @@ export const { handle, signIn, signOut } = SvelteKitAuth({
91110
import { ExpressAuth } from "@auth/express"
92111
import GitLab from "@auth/express/providers/gitlab"
93112

94-
app.use("/auth/*", ExpressAuth({ providers: [GitLab] }))
113+
app.use(
114+
"/auth/*",
115+
ExpressAuth({
116+
providers: [
117+
GitLab,
118+
GitLab({
119+
baseUrl: "https://gitlab.example.com",
120+
}),
121+
],
122+
})
123+
)
95124
```
96125

97126
</Code.Express>

packages/core/src/providers/gitlab.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,26 @@ export interface GitLabProfile extends Record<string, any> {
109109
* :::
110110
*/
111111
export default function GitLab<P extends GitLabProfile>(
112-
options: OAuthUserConfig<P>
112+
options: OAuthUserConfig<P> & {
113+
/**
114+
* @default "https://gitlab.com"
115+
*/
116+
baseUrl?: URL | string
117+
}
113118
): OAuthConfig<P> {
119+
const baseUrl = options.baseUrl ?? "https://gitlab.com"
120+
const url = new URL(baseUrl.toString())
121+
114122
return {
115123
id: "gitlab",
116124
name: "GitLab",
117125
type: "oauth",
118-
authorization: "https://gitlab.com/oauth/authorize?scope=read_user",
119-
token: "https://gitlab.com/oauth/token",
120-
userinfo: "https://gitlab.com/api/v4/user",
126+
authorization: `${url}oauth/authorize?scope=read_user`,
127+
token: `${url}oauth/token`,
128+
userinfo: `${url}api/v4/user`,
121129
profile(profile) {
122130
return {
123-
id: profile.sub?.toString(),
131+
id: profile.sub?.toString() ?? profile.id?.toString(),
124132
name: profile.name ?? profile.username,
125133
email: profile.email,
126134
image: profile.avatar_url,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { expect, it } from "vitest"
2+
import GitLab from "../../src/providers/gitlab"
3+
4+
it("GitLab should handle baseURL correctly", () => {
5+
const config = GitLab({
6+
baseUrl: "https://gitlab.example.com",
7+
})
8+
expect(config.id).toBe("gitlab")
9+
expect(config.name).toBe("GitLab")
10+
expect(config.type).toBe("oauth")
11+
expect(config.authorization).toEqual(
12+
"https://gitlab.example.com/oauth/authorize?scope=read_user"
13+
)
14+
expect(config.token).toEqual("https://gitlab.example.com/oauth/token")
15+
expect(config.userinfo).toEqual("https://gitlab.example.com/api/v4/user")
16+
})

0 commit comments

Comments
 (0)