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.
Before you start
Section titled “Before you start”- A working social sign-in from Sign in with Google and Apple.
- A Meta app with the Facebook sign-in product and the permissions
emailandpublic_profile, forsocial.facebook.
- Add
https://<your api host>/auth/social/facebook/callbackto the valid OAuth redirect URIs of the Facebook sign-in product. - Copy the app id and the app secret to your secret store.
- Set
social.facebookwithclientIdandclientSecret. - Link to
/auth/social/facebook/start?next=/homefrom the sign-in page.
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" }, }, });}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"); });});How it behaves
Section titled “How it behaves”- 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 fromGET /me. - The subject is the app-scoped user
id, the key that Better Auth keeps. A Better Authaccountrow of the providerfacebooklinks 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.
Residuals
Section titled “Residuals”- 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.0to a newer version.
If it fails
Section titled “If it fails”oauth_failed: the token call or the profile call failed. Check the app id, the secret, and the redirect URI.oauth_denied: the person refused at Facebook.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.