Skip to content

Sign in with Facebook

For: an app developer whose users have Facebook accounts.

What it does: social.facebook adds a “Continue with Facebook” button. Core finds or makes the user by the app-scoped Facebook user id.

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

  1. Add https://<your api host>/auth/social/facebook/callback to the valid OAuth redirect URIs of the Facebook sign-in product.
  2. Copy the app id and the app secret to your secret store.
  3. Set social.facebook with clientId and clientSecret.
  4. Link to /auth/social/facebook/start?next=/home from the sign-in page.
The Facebook preset in the config
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
export function createFacebookAuth(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 app id and the app secret of the Meta app. Read them from your secret store.
facebook: { clientId: "1234567890", clientSecret: "facebook-app-secret" },
},
});
}
The start as a test
import { describe, expect, it } from "vitest";
import { call, harness } from "../support/harness.ts";
import { createFacebookAuth } from "./facebook.ts";
describe("sign in with Facebook", () => {
it("sends the person to the provider with the scope and a PKCE challenge", async () => {
const auth = createFacebookAuth(harness());
const res = await call(auth, "GET", "/auth/social/facebook/start?next=/home");
expect(res.status).toBe(302);
const to = new URL(res.headers.get("location") ?? "");
expect(to.origin + to.pathname).toBe("https://www.facebook.com/v26.0/dialog/oauth");
expect(to.searchParams.get("scope")).toBe("email public_profile");
expect(to.searchParams.get("redirect_uri")).toBe(
"https://api.example.com/auth/social/facebook/callback",
);
expect(to.searchParams.get("code_challenge_method")).toBe("S256");
});
});
  • Each call uses the Graph API version v26.0. Meta says a call with no version uses the version of the app dashboard.
  • The scope is email public_profile. Core reads the profile from GET /me.
  • The subject is the app-scoped user id, the key that Better Auth keeps. A Better Auth account row of the provider facebook links with no change.
  • A Facebook address never counts as verified. The User reference names no field that proves the mailbox. So a Facebook address links no user that exists.
  • Core makes the code exchange itself with the app secret, so the token is bound to this app.
  • A Facebook address can fill a new user, unverified. The user then proves the address by mail.
  • Meta’s page of the manual sign-in flow names no PKCE, and core sends a PKCE challenge to each provider. Before you go live, confirm that Facebook accepts it.
  • Core posts the code exchange as a form. Before you go live, confirm that Facebook accepts the exchange as a POST.
  • Meta retires each Graph API version on its own date. A later release of core moves v26.0 to a newer version.