Skip to content

Social sign-in on preview deployments

For: an app developer whose host makes a deployment on a new host for each branch or pull request.

What it does: this page gives the two ways to register the callback of a preview host at a provider. Core sends each provider the callback of the host where the sign-in started.

  1. Give each preview deployment its own baseURL, appURL, and trustedOrigins, from the settings of that deployment.
  2. Choose path 1 or path 2 below for each provider.
  3. Path 1: make an OAuth app at the provider for each preview host. Register only the callback of that host. Put its client id and secret in the settings of that deployment.
  4. Path 2: use a provider that takes several callback URLs for one client. Register the callback of each preview host on the one client.
  5. Register the callback <API origin>/auth/social/<provider>/callback, or the path of redirectURI on that origin.
  6. Before you merge, remove the callback of a preview host that has gone.
The settings of one deployment
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
/** The values that differ per deployment. Each preview deployment sets its own. */
export interface DeployEnv {
/** The origin of the API of this deployment, such as https://pr-42.preview.example.com. */
API_ORIGIN: string;
/** The origin of the app of this deployment. */
APP_ORIGIN: string;
/**
* The OAuth client of this host. Path 1: an OAuth app of its own, with the
* callback of this host only. Path 2: one client of a provider that takes
* several callbacks, with the callback of each host registered.
*/
GITHUB_CLIENT_ID: string;
GITHUB_CLIENT_SECRET: string;
}
/** The settings of production. Read them from the settings of each deployment. */
const PRODUCTION: DeployEnv = {
API_ORIGIN: "https://api.example.com",
APP_ORIGIN: "https://app.example.com",
GITHUB_CLIENT_ID: "github-client-id",
GITHUB_CLIENT_SECRET: "github-client-secret",
};
export function createDeploymentAuth(
deps: { storage: StorageAdapter; mail: MailPort; secret: string },
env: DeployEnv = PRODUCTION,
) {
return createAuth({
// The callback is <API_ORIGIN>/auth/social/github/callback. Register it at the provider.
baseURL: env.API_ORIGIN,
appURL: env.APP_ORIGIN,
trustedOrigins: [env.APP_ORIGIN],
secret: deps.secret,
storage: deps.storage,
mail: deps.mail,
social: {
github: { clientId: env.GITHUB_CLIENT_ID, clientSecret: env.GITHUB_CLIENT_SECRET },
},
});
}
The callback of a preview host as a test
import { describe, expect, it } from "vitest";
import { harness } from "../support/harness.ts";
import { createDeploymentAuth, type DeployEnv } from "./preview-deployments.ts";
const PREVIEW: DeployEnv = {
API_ORIGIN: "https://pr-42.preview.example.com",
APP_ORIGIN: "https://pr-42-app.preview.example.com",
// Path 1: the OAuth app of this preview host.
GITHUB_CLIENT_ID: "preview-42-client-id",
GITHUB_CLIENT_SECRET: "preview-42-client-secret",
};
describe("social sign-in on a preview deployment", () => {
it("sends the callback of the preview host, never of production", async () => {
const auth = createDeploymentAuth(harness(), PREVIEW);
const res = await auth.handle(
new Request("https://pr-42.preview.example.com/auth/social/github/start"),
);
expect(res.status).toBe(302);
const to = new URL(res.headers.get("location") ?? "");
expect(to.searchParams.get("client_id")).toBe("preview-42-client-id");
expect(to.searchParams.get("redirect_uri")).toBe(
"https://pr-42.preview.example.com/auth/social/github/callback",
);
});
});
  • Core sends redirect_uri on the origin of the request where the sign-in started. With a baseURL list, that is the picked entry.
  • A redirectURI override is a path under basePath on the origin of an entry of baseURL. Core serves that path as the callback of the provider.
  • The binding cookie ties the callback to the browser that started the sign-in, on the same host. So the callback must come back to that host.
  • The state row, the PKCE verifier, and the binding cookie stay on one host for the whole flow.

Better Auth’s oauth-proxy plugin lets a preview host use the callback of production. Production gets the callback, and sends the result on to the preview host. Core declines it, because it routes the state of a sign-in through a second host.

  • A proxy needs production to send a sign-in result to another host. Then a preview build, which runs code that nobody reviewed yet, can get it.
  • The binding cookie of a flow is on the host that started it. A callback on production cannot read it, so it cannot tie the callback to that browser.
  • Each path above keeps the whole flow on one host. A preview host then holds only its own sign-ins.
  • Path 1 needs one OAuth app per preview host. A host with many previews needs many apps, or a small set of stable preview hosts.
  • Path 2 leaves the callbacks of old previews on the client until you remove them. A host that someone else takes later can then get a code for that client.
  • A passkey binds to its rpID. A preview host outside the domain of rpID cannot use the passkeys of production.
  • The provider shows its own error page and does not come back: the callback of this host is not registered. Register it, as in path 1 or path 2.
  • oauth_denied: the provider sent an error to the callback. Check the callback that the OAuth app of this host lists.
  • oauth_failed: the code exchange failed. Check that the client id and secret are those of the OAuth app of this host.
  • invalid_token: the callback came to another host than the start, so the binding cookie was absent. Start the sign-in on the host that gets the callback.
  • host_not_allowed: with a baseURL list, the preview host is not in the list. Add its origin.