Skip to content

Sign in with X

For: an app developer whose users have X accounts.

What it does: social.twitter adds a “Continue with X” button. Core finds or makes the user by the X user id. The provider name is twitter, as in Better Auth.

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

  1. Add https://<your api host>/auth/social/twitter/callback to the callback URIs of the X app.
  2. Copy the OAuth 2.0 client id and client secret to your secret store.
  3. Set social.twitter with clientId and clientSecret.
  4. Link to /auth/social/twitter/start?next=/home from the sign-in page.
The X preset in the config
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
export function createXAuth(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: {
// The provider name is twitter, as in Better Auth. The button says X.
// Read the secret from your secret store. The placeholders only build the config.
twitter: { clientId: "x-client-id", clientSecret: "x-client-secret" },
},
});
}
The start as a test
import { describe, expect, it } from "vitest";
import { call, harness } from "../support/harness.ts";
import { createXAuth } from "./x.ts";
describe("sign in with X", () => {
it("sends the person to the provider with the scope and a PKCE challenge", async () => {
const auth = createXAuth(harness());
const res = await call(auth, "GET", "/auth/social/twitter/start?next=/home");
expect(res.status).toBe(302);
const to = new URL(res.headers.get("location") ?? "");
expect(to.origin + to.pathname).toBe("https://x.com/i/oauth2/authorize");
expect(to.searchParams.get("scope")).toBe("users.read tweet.read users.email");
expect(to.searchParams.get("redirect_uri")).toBe(
"https://api.example.com/auth/social/twitter/callback",
);
expect(to.searchParams.get("code_challenge_method")).toBe("S256");
});
});
  • The scope is users.read tweet.read users.email.
  • Core sends the client id and secret to the token URL as HTTP Basic, as X asks of a confidential client.
  • The subject is the user id of GET /2/users/me, the key that Better Auth keeps. A Better Auth account row of the provider twitter links with no change.
  • A second read of GET /2/users/me asks for confirmed_email. Only that field gives an address, and it counts as verified.
  • When the second read fails, the profile has no address. An X account that signed in before still signs in.
  • A new person with no address from X gets email_required: core makes a user from a preset only with an address. So ask X for the email access of the app.
  • X defines confirmed_email as the confirmed address of the user. Core’s tests use recorded answers. Before you go live, confirm that X gives no confirmed_email for an address that is not confirmed.
  • oauth_failed: the token call or the first profile read failed. Check that the app is a confidential client, and check the secret.
  • oauth_denied: the person refused at X.
  • email_required: X gave no address for a new person. Ask X for the email access of the app.
  • 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.