Skip to content

Sign in with GitLab

For: an app developer whose users have accounts on gitlab.com or on a GitLab instance of their company.

What it does: social.gitlab adds a “Continue with GitLab” button. Core finds or makes the user by the numeric GitLab user id.

Before you rely on this preset, sign in once with a real account.

  1. Set https://<your api host>/auth/social/gitlab/callback as the redirect URI of the GitLab application.
  2. Copy the application id and the secret to your secret store.
  3. Set social.gitlab with clientId and clientSecret.
  4. For your own instance, set issuer to its https: origin.
  5. Link to /auth/social/gitlab/start?next=/home from the sign-in page.
The GitLab preset in the config
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
export function createGitlabAuth(deps: {
storage: StorageAdapter;
mail: MailPort;
secret: string;
}) {
return createAuth({
baseURL: "https://api.example.com",
appURL: "https://app.example.com",
trustedOrigins: ["https://app.example.com"],
secret: deps.secret,
storage: deps.storage,
mail: deps.mail,
social: {
// Read the secret from your secret store. The placeholders only build the config.
gitlab: {
clientId: "gitlab-application-id",
clientSecret: "gitlab-secret",
// Your own GitLab instance. Leave it out for gitlab.com.
issuer: "https://gitlab.example.org",
},
},
});
}
The start as a test
import { describe, expect, it } from "vitest";
import { call, harness } from "../support/harness.ts";
import { createGitlabAuth } from "./gitlab.ts";
describe("sign in with GitLab", () => {
it("sends the person to the provider with the scope and a PKCE challenge", async () => {
const auth = createGitlabAuth(harness());
const res = await call(auth, "GET", "/auth/social/gitlab/start?next=/home");
expect(res.status).toBe(302);
const to = new URL(res.headers.get("location") ?? "");
expect(to.origin + to.pathname).toBe("https://gitlab.example.org/oauth/authorize");
expect(to.searchParams.get("scope")).toBe("read_user");
expect(to.searchParams.get("redirect_uri")).toBe(
"https://api.example.com/auth/social/gitlab/callback",
);
expect(to.searchParams.get("code_challenge_method")).toBe("S256");
});
});
  • issuer is https://gitlab.com by default. Another value is an https: origin with no path and no trailing slash.
  • Core posts the client secret to the issuer. So createAuth refuses an IP address, a local name, a host with an empty label such as a..example.com, or the host of baseURL. Core uses this one host rule for each URL that it fetches or posts to.
  • The scope is read_user. Core reads the profile from GET /api/v4/user.
  • The subject is the numeric id, the key that Better Auth keeps. A Better Auth account row of the provider gitlab links with no change.
  • A user whose state is not active, or who is locked, answers oauth_failed.
  • A GitLab address never counts as verified. The Users API names no field that proves the mailbox. So a GitLab address links no user that exists.
  • A GitLab address can fill a new user, unverified. The user then proves the address by mail.
  • A first GitLab sign-in never links to an account that holds the same address. The callback answers email_taken_unverified.
  • oauth_failed: the token call or the profile call failed, or the user is not active. Check the issuer, the application id, and the secret.
  • oauth_denied: the person refused at GitLab.
  • email_taken_unverified: another user holds the address.
  • invalid_token: the state is spent or old, or the callback came to another browser. Start again.
  • sign_up_closed: the sign-up rule refused a new user.