How does Sign in with Google work?
Tapping "Sign in with Google" takes a couple of seconds. In that time the app asks Google who the user is, Google checks the user's password on its own page, and the app receives something it can verify without ever seeing the password. Two specifications cover this: OAuth 2.0 and OpenID Connect.
OAuth and OpenID Connect
OAuth 2.0, defined in RFC 6749, is about access. It lets an app act on a user's behalf, for example reading their calendar, without the user giving the app their password. The specification describes the problems with sharing passwords directly: every third-party app would have to store the password, and compromising any one of them would expose the account. OAuth replaces the password with an access token.
An access token says what the app may do. It does not, on its own, give the app a standard way to know who the user is. OpenID Connect adds that. It describes itself as an identity layer on top of OAuth 2.0, and its main addition is the ID token: a signed JWT containing claims about the sign-in, including the issuer (iss), the user's identifier (sub), the intended audience (aud), and expiry and issue times.
The redirect flow
The redirect flow works on the web and on phones. With Supabase it goes like this. The app opens the system browser at Supabase's authorize URL. Supabase sends the browser to Google. The user signs in on Google's own page. Google sends the browser back to a registered callback URL with a short-lived authorization code. Supabase exchanges that code with Google, server to server, for tokens. Finally it returns the user to the app. In the PKCE flow it sends a second code of its own, which the app exchanges for a Supabase session with exchangeCodeForSession.
The request to Google carries the app's client ID, the redirect URI, the scopes, a state value and, with PKCE, a code challenge. For a sign-in the scopes are usually openid, email and profile. The openid scope is what makes it an OpenID Connect request; without it, no ID token comes back.
The only thing that passes through the browser is the code, and the code is worthless without a way to prove who requested it.
The flow uses the system browser rather than a web view inside the app. RFC 8252, which covers OAuth for native apps, explains why: an app hosting the login page in its own web view could record every keystroke on the login form.
Why there is a callback URL
The authorization code goes wherever the redirect URI says. If Google sent codes to any URL, anyone could start a sign-in with an app's client ID and ask for the code to be delivered to their own server. So Google only redirects to URIs registered for that client in advance. Its documentation states that the value must exactly match a registered URI, including scheme, case and trailing slash, and returns a redirect_uri_mismatch error otherwise.
The state parameter adds a second check. The app sends a random value with the request and compares it with the value that comes back, which confirms the response belongs to a sign-in the app started. RFC 6749 recommends it for preventing cross-site request forgery.
PKCE in one picture
On a phone, the redirect back into the app often uses a custom URL scheme such as myapp://. Another app on the same device can register the same scheme and receive the redirect, along with the code. PKCE, defined in RFC 7636, makes an intercepted code useless.
Before starting the sign-in, the app generates a random string of 43 to 128 characters called the code verifier and keeps it. It sends only a SHA-256 hash of it, the code challenge, with the authorization request. The server stores the challenge alongside the code it issues. When the app exchanges the code, it sends the original verifier. The server hashes it and compares. An app that intercepted the code never saw the verifier, so its exchange fails.
RFC 8252 requires public native app clients to use PKCE. In Supabase's PKCE flow, the code is valid for five minutes and can be exchanged only once.
The native ID token flow
On Android and iOS there is a second approach. The app uses Google's own sign-in on the device (Credential Manager on Android, the Google Sign-In library on iOS). When the user picks an account, Google returns an ID token directly to the app. The app passes it to Supabase with signInWithIdToken.
Supabase then checks the token and, if it is valid, creates a Supabase session. A nonce can be included so that a token issued for one sign-in cannot be replayed in another. For Expo apps, Supabase's documentation points to this native flow.
What Google sends back
The ID token is a JWT. Its payload includes iss (Google), aud (the app's client ID), sub (the user's Google ID), exp and iat, and usually email, email_verified, name and picture depending on the scopes requested. Google's documentation advises using sub, not email, as the unique identifier, because an email address can change while sub is never reassigned.
In this setup the ID token is used once, at sign-in, to establish who the user is. It is not what the app sends with later requests. After verification, Supabase issues its own access and refresh tokens. From then on the app works with Supabase's session, not Google's tokens.
What a sign-in proves
A verified ID token proves that Google authenticated a particular Google account. It does not prove a real customer is behind it. After uploads to Google Play, the pre-launch test robots signed up to my apps, and every one of those sign-ins was valid. Sign-ups and real users are different numbers.
Sources
- IETF: RFC 6749, The OAuth 2.0 Authorization Framework
- OpenID Foundation: OpenID Connect Core 1.0
- IETF: RFC 8252, OAuth 2.0 for Native Apps
- IETF: RFC 7636, Proof Key for Code Exchange
- Google docs: OAuth 2.0 for web server applications
- Google docs: OpenID Connect
- Supabase docs: Login with Google
- Supabase docs: PKCE flow