Bỏ qua, tới nội dung chính

Kiến trúc · ADR 0025

Google OAuth sign-in

Trạng thái
accepted
Ngày
2026-09-28
Người quyết định
Alex

Context

ADR-0021 dropped Google OAuth to save time and ADR-0023 shipped the social buttons as visual placeholders, keeping the User.googleSub column ready. The owner has now provided a Google OAuth client, so Google sign-in goes live. Apple and Twitter stay placeholders.

Decision

  • Server-side authorization-code flow, split the project's way. The web owns the OAuth dance (it holds the client secret): GET /session/google mints a state, stores it in a short-lived httpOnly cookie, and redirects to Google; GET /session/google/callback checks state, exchanges the code for a Google id_token, and posts it to the API. The API verifies the id_token against Google's keys with google-auth-library, checking the audience is our GOOGLE_CLIENT_ID — nothing in the token is trusted before that. The web then writes the same httpOnly session cookies as email login. The token never reaches the browser.
  • Account resolution: match by googleSub → sign in; else match by email → link (set googleSub, emailVerifiedVia = GOOGLE); else create a STUDENT with passwordHash = null. Google's email must be email_verified — that is what makes a GOOGLE-verified address (unlike a SELF_ASSERTED one) eligible to auto-join an EMAIL_DOMAIN organization later.
  • Env: the web reads GOOGLE_CLIENT_ID + GOOGLE_CLIENT_SECRET; the API reads only GOOGLE_CLIENT_ID (optional — when unset, Google sign-in is simply unavailable and the service still boots). Redirect URI registered in the console: <origin>/session/google/callback for each origin (http://localhost:3100, http://localhost:8080, and production).

Consequences

  • The real, tested email+password flow is unchanged; Google is an additional door that reuses the same session issuing. A POST /auth/google endpoint verifies and returns the session envelope.
  • No token or client secret is exposed to the browser. Failures (denied consent, bad state, verification error) land the user back on /login?error=google.
  • Apple/Twitter remain placeholders; when they land, only social-buttons.tsx and their own routes change.