GitLab

GitLab 提供者的设置与使用。

获取 GitLab 凭证

要使用 GitLab 登录,您需要一个客户端 ID 和客户端密钥。请参考 GitLab OAuth 文档。

请确保将重定向 URL 设置为 http://localhost:3000/api/auth/callback/gitlab,用于本地开发。生产环境中,应将其设置为应用程序的 URL。如果修改了认证路由的基础路径,需要相应地更新重定向 URL。

配置提供者

要配置该提供者,需要导入提供者并将其传递给认证实例的 socialProviders 选项。

auth.ts
import { betterAuth } from "better-auth"

export const auth = betterAuth({
    socialProviders: {
        gitlab: { 
            clientId: process.env.GITLAB_CLIENT_ID as string, 
            clientSecret: process.env.GITLAB_CLIENT_SECRET as string, 
            issuer: process.env.GITLAB_ISSUER as string, 
        }, 
    },
})

配置选项

  • clientId:您的 GitLab 应用程序的客户端 ID
  • clientSecret:您的 GitLab 应用程序的客户端密钥
  • issuer:(可选)您的 GitLab 实例的 URL。用于自托管的 GitLab 服务器。
    • 默认值:"https://gitlab.com"(GitLab.com)
    • 示例:"https://gitlab.company.com"

当使用自托管的 GitLab 实例时,issuer 选项非常有用。如果您使用的是 GitLab.com,可以省略此选项,因为它的默认值为 https://gitlab.com。

自托管 GitLab 示例

auth.ts
export const auth = betterAuth({
    socialProviders: {
        gitlab: {
            clientId: process.env.GITLAB_CLIENT_ID as string,
            clientSecret: process.env.GITLAB_CLIENT_SECRET as string,
            issuer: "https://gitlab.company.com", // 您的自托管 GitLab URL
        },
    },
})

使用 GitLab 登录

要使用 GitLab 登录,可以使用客户端提供的 signIn.social 函数。signIn 函数接受一个包含以下属性的对象:

  • provider:要使用的提供者,应设置为 gitlab。
auth-client.ts
import { createAuthClient } from "better-auth/client"
const authClient =  createAuthClient()

const signIn = async () => {
    const data = await authClient.signIn.social({
        provider: "gitlab"
    })
}