GitLab

Get your GitLab credentials

To use GitLab sign in, you need a client ID and client secret. GitLab OAuth documentation.

Make sure to set the redirect URL to http://localhost:3000/api/auth/callback/gitlab for local development. For production, you should set it to the URL of your application. If you change the base path of the auth routes, you should update the redirect URL accordingly.

Configure the provider

To configure the provider, you need to import the provider and pass it to the social_providers option of the auth instance.

config/auth.rb
require "better_auth"

auth = BetterAuth.auth(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
social_providers: {
gitlab: BetterAuth::SocialProviders.gitlab(
  client_id: ENV.fetch("GITLAB_CLIENT_ID"),
  client_secret: ENV.fetch("GITLAB_CLIENT_SECRET"),
  issuer: ENV.fetch("GITLAB_ISSUER", "https://gitlab.com")
)
}
)

Configuration Options

  • client_id: Your GitLab application's Client ID
  • client_secret: Your GitLab application's Client Secret
  • issuer: (Optional) The URL of your GitLab instance. Use this for self-hosted GitLab servers.
    • Default: "https://gitlab.com" (GitLab.com)
    • Example: "https://gitlab.company.com"

The issuer option is useful when using a self-hosted GitLab instance. If you're using GitLab.com, you can omit this option as it defaults to https://gitlab.com.

Example with self-hosted GitLab

config/auth.rb
require "better_auth"

auth = BetterAuth.auth(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
social_providers: {
gitlab: BetterAuth::SocialProviders.gitlab(
  client_id: ENV.fetch("GITLAB_CLIENT_ID"),
  client_secret: ENV.fetch("GITLAB_CLIENT_SECRET"),
  issuer: ENV.fetch("GITLAB_ISSUER", "https://gitlab.com")
)
}
)

Sign In with GitLab

To sign in with GitLab, call auth.api.sign_in_social on your Ruby auth instance. The endpoint body takes the following properties:

  • provider: The provider to use. It should be set to gitlab.
server.rb
response = auth.api.sign_in_social(
body: {
provider: "gitlab",
callback_url: "/dashboard",
error_callback_url: "/login",
disable_redirect: true
}
)

redirect_url = response.fetch(:url)

On this page