Skip to content

Sign in with Google and Apple

For: an app developer who wants a “Continue with Google” button and a “Continue with Apple” button.

What it does: core runs the authorization code flow with PKCE, a state value, and a nonce, and checks the ID token of the provider.

  • A working setup from a getting started tutorial.
  • A Google OAuth client, with its client id and secret. Social lists the keys that hold them.
  • An Apple Services ID, a Sign in with Apple key, its key id, and your team id. Social lists the keys that hold them.
  1. At Google, register the redirect URI https://api.example.com/auth/social/google/callback.
  2. At Apple, register the return URL https://api.example.com/auth/social/apple/callback.
  3. Put the secrets in your secret store, and read them into social.google and social.apple.
The Google and Apple config
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
/** The provider secrets. Read them from your secret store. The placeholders only build the config. */
export interface SocialEnv {
GOOGLE_CLIENT_ID: string;
GOOGLE_CLIENT_SECRET: string;
APPLE_SERVICES_ID: string;
APPLE_TEAM_ID: string;
APPLE_KEY_ID: string;
/** The PKCS#8 PEM of the Sign in with Apple key. */
APPLE_PRIVATE_KEY: string;
}
const PLACEHOLDERS: SocialEnv = {
GOOGLE_CLIENT_ID: "google-client-id",
GOOGLE_CLIENT_SECRET: "google-client-secret",
APPLE_SERVICES_ID: "com.example.web",
APPLE_TEAM_ID: "TEAMID1234",
APPLE_KEY_ID: "KEYID12345",
APPLE_PRIVATE_KEY: "-----BEGIN PRIVATE KEY-----\n…\n-----END PRIVATE KEY-----",
};
export function createSocialAuth(
deps: { storage: StorageAdapter; mail: MailPort; secret: string },
env: SocialEnv = PLACEHOLDERS,
) {
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: {
google: { clientId: env.GOOGLE_CLIENT_ID, clientSecret: env.GOOGLE_CLIENT_SECRET },
// Core signs a client secret of five minutes for each token exchange,
// so no stored secret expires. Rotate the key in the Apple portal.
apple: {
clientId: env.APPLE_SERVICES_ID,
teamId: env.APPLE_TEAM_ID,
keyId: env.APPLE_KEY_ID,
privateKey: env.APPLE_PRIVATE_KEY,
},
},
});
}
  1. Link each button to /auth/social/google/start?next=/home or /auth/social/apple/start?next=/home on the API.

  2. Core’s own sign-in page shows the buttons of every configured provider. A custom page must show them too.

  3. For Google One Tap or the native Apple sheet, ask POST /auth/social/<provider>/id-token/nonce for a nonce. Give providerNonce to the provider, and post the ID token with the raw nonce to POST /auth/social/<provider>/id-token. Sign in with Google One Tap and Sign in with the native Apple sheet give the steps.

The start of each flow and the nonce of One Tap as a test
import { describe, expect, it } from "vitest";
import { call, harness } from "../support/harness.ts";
import { createSocialAuth } from "./social.ts";
describe("sign in with Google and Apple", () => {
it("sends the browser to Google with state, nonce, PKCE, and a binding cookie", async () => {
const auth = createSocialAuth(harness());
const res = await call(auth, "GET", "/auth/social/google/start?next=/home");
expect(res.status).toBe(302);
const to = new URL(res.headers.get("location") ?? "");
expect(`${to.origin}${to.pathname}`).toBe("https://accounts.google.com/o/oauth2/v2/auth");
expect(to.searchParams.get("redirect_uri")).toBe(
"https://api.example.com/auth/social/google/callback",
);
expect(to.searchParams.get("code_challenge_method")).toBe("S256");
expect(to.searchParams.get("state")).not.toBeNull();
expect(to.searchParams.get("nonce")).not.toBeNull();
const cookie =
res.headers.getSetCookie().find((c) => c.startsWith("__Secure-tempo_session_oauth=")) ?? "";
expect(cookie).toContain("Path=/auth/social");
expect(cookie).toContain("SameSite=None");
});
it("asks Apple for a form post to its own callback", async () => {
const auth = createSocialAuth(harness());
const res = await call(auth, "GET", "/auth/social/apple/start");
const to = new URL(res.headers.get("location") ?? "");
expect(to.origin).toBe("https://appleid.apple.com");
expect(to.searchParams.get("response_mode")).toBe("form_post");
expect(to.searchParams.get("redirect_uri")).toBe(
"https://api.example.com/auth/social/apple/callback",
);
});
it("gives One Tap the hash of the nonce, and keeps the raw nonce for the post", async () => {
const auth = createSocialAuth(harness());
const res = await call(auth, "POST", "/auth/social/google/id-token/nonce", {});
expect(res.status).toBe(200);
const { nonce, providerNonce } = (await res.json()) as {
nonce: string;
providerNonce: string;
};
// Give Google providerNonce: google.accounts.id.initialize({ nonce: providerNonce }).
// Post { idToken, nonce } to /auth/social/google/id-token. Never give nonce to Google.
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(nonce));
const hex = [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, "0")).join("");
expect(providerNonce).toBe(hex);
});
});
  • A binding cookie of 10 minutes ties the callback to the browser that started the flow.
  • Apple posts its callback across sites, so the binding cookie is SameSite=None and lives on the path /auth/social.
  • Core signs a new Apple client secret of five minutes for each token exchange. Rotate the key in the Apple portal, then change privateKey.
  • Core links a social identity to an existing user only when the provider reports the address as verified and it equals the user’s address.
  • An address that a verified provider proves claims an unverified account. The claim rule tells what core deletes.
  • Apple sends the name on the first sign-in only. Core stores it then.
  • The start has one limit for every provider: social-start:ip:<ip> 30 per 15 minutes.
  • Apple’s callback form post has its own limit, social-callback:ip:<ip> 30 per 15 minutes. Core counts a post before it reads the form.
  • A user with the second factor on goes to the challenge page, with no cookie. Turn on the second factor tells the next step.

A failure sends the browser to appURL with ?auth_error=<code>:

  • oauth_denied: the person refused, or the provider sent an error.
  • oauth_failed: the code exchange or the ID token failed. Check the client secret and the redirect URI.
  • email_required: the profile has no address.
  • email_taken_unverified: an unverified account has the address, and the provider did not prove it.
  • invalid_token: the state or the binding cookie is missing. A second start in another tab replaces the cookie.
  • token_expired: the flow took more than 10 minutes. Start again.
  • rate_limited: too many starts from the IP.