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.
Before you start
Section titled “Before you start”- A working social sign-in from Sign in with Google and Apple.
- An application on the GitLab instance with the scope
read_user, forsocial.gitlab.
- Set
https://<your api host>/auth/social/gitlab/callbackas the redirect URI of the GitLab application. - Copy the application id and the secret to your secret store.
- Set
social.gitlabwithclientIdandclientSecret. - For your own instance, set
issuerto itshttps:origin. - Link to
/auth/social/gitlab/start?next=/homefrom the sign-in page.
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", }, }, });}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"); });});How it behaves
Section titled “How it behaves”issuerishttps://gitlab.comby default. Another value is anhttps:origin with no path and no trailing slash.- Core posts the client secret to the issuer. So
createAuthrefuses an IP address, a local name, a host with an empty label such asa..example.com, or the host ofbaseURL. Core uses this one host rule for each URL that it fetches or posts to. - The scope is
read_user. Core reads the profile fromGET /api/v4/user. - The subject is the numeric
id, the key that Better Auth keeps. A Better Authaccountrow of the providergitlablinks with no change. - A user whose
stateis notactive, or who islocked, answersoauth_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.
Residuals
Section titled “Residuals”- 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.
If it fails
Section titled “If it fails”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.