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.
Before you start
Section titled “Before you start”- A working social sign-in from Sign in with Google and Apple.
- An X app with OAuth 2.0 as a confidential client, for
social.twitter. Ask X for the email access of the app.
- Add
https://<your api host>/auth/social/twitter/callbackto the callback URIs of the X app. - Copy the OAuth 2.0 client id and client secret to your secret store.
- Set
social.twitterwithclientIdandclientSecret. - Link to
/auth/social/twitter/start?next=/homefrom the sign-in page.
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" }, }, });}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"); });});How it behaves
Section titled “How it behaves”- 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
idofGET /2/users/me, the key that Better Auth keeps. A Better Authaccountrow of the providertwitterlinks with no change. - A second read of
GET /2/users/measks forconfirmed_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.
Residuals
Section titled “Residuals”- X defines
confirmed_emailas the confirmed address of the user. Core’s tests use recorded answers. Before you go live, confirm that X gives noconfirmed_emailfor an address that is not confirmed.
If it fails
Section titled “If it fails”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.