ResourceInformation

Library › Developer Pain

🔑 Developer Pain

OAuth Explained: How “Sign in With Google” Works

Library · 30 November 2026 · 6 min read

OAuth Explained: How “Sign in With Google” Works
▶ Prefer watching? This is also a 2:17 animated video.Subscribe on YouTube

You tap Sign in with Google and two seconds later you're in. The app never saw your password. Behind that one tap are two redirects, a one-time code, a secret and three tokens, and one wrong character in the setup gives you redirect_uri_mismatch. Here's the whole thing, slowly, with moving pictures.

⏱ Developer Pain series: the integrations and errors that waste the most developer hours. Next: see what's coming.

First, two names people mix up

NameAnswersYou get
OAuth 2.0"What may this app do for the user?"an access token
OpenID Connect (built on OAuth)"Who is this user?"an ID token

"Sign in with Google" is OpenID Connect. It uses OAuth's steps, plus an ID token that says who signed in.

The whole journey

🌐 Your browser🗄️ MyApp server🔐 Google① tap “Sign in with Google”② go to Googleclient_id, scope, redirect_uri, state③ sign in and approveyour password goes only to Google④ back to redirect_uricode + state⑤ here's the codeserver checks state first⑥ code + client secretserver to server⑦ ID token + access token✓ token checked: signed in
Steps ① to ⑤ pass through your browser. Steps ⑥ and ⑦ happen directly between the two servers, out of sight.

1. The app sends you to Google

The app doesn't show its own password box. It sends your browser to Google with a link that looks like this:

https://accounts.google.com/o/oauth2/v2/auth
  ?client_id=1234.apps.googleusercontent.com   who is asking
  &response_type=code                          send back a code
  &scope=openid%20email%20profile              what it wants
  &redirect_uri=https://myapp.com/callback     where to return
  &state=x7Qp…                                 a random value, checked later

Google only returns people to addresses the developer registered in advance. Otherwise anyone could build a link that sends your code to their own site. The match must be exact: scheme, upper and lower case, and even the last slash.

Try it: redirect URI checker

Paste the address you registered and the one your app sends. The check runs in your browser and nothing is sent anywhere.

Press "Check"

2. You approve on Google's page

You're now on accounts.google.com, so your password goes to Google and nowhere else. If needed, Google asks whether to share what's in scope: here, your name, picture and email. Say yes, and Google sends your browser back:

https://myapp.com/callback?code=4/0Ab…&state=x7Qp…

The app first checks that state is the same value it created. If it isn't, this return didn't start from your sign-in, so the app must stop. That blocks forged sign-ins (a CSRF attack).

3. The server swaps the code for tokens

The code isn't a login. It's short-lived and works once, and by itself it isn't enough:

STOLEN CODEa code can leak from the address bar, logs or history🕵️someone with a stolen code🔐Google's token addresscodeno secretrefused ✖
A code passes through the browser, so it can leak. Without the client secret, Google refuses to swap it.
THE REAL SERVERthe code plus a secret that never leaves the server🗄️MyApp server🔐Google's token addresscodeclient_secrettokens ✓
The app's server adds its secret and calls Google directly. The tokens never appear in an address bar.
POST https://oauth2.googleapis.com/token
code=4/0Ab…&client_id=…&client_secret=…
&redirect_uri=https://myapp.com/callback&grant_type=authorization_code

Apps that can't keep a secret (mobile, desktop, single-page apps) use PKCE: the app makes a random value for each sign-in, sends its hash first and shows the original when swapping the code. A thief who has only the code can't finish the swap. Current security advice (RFC 9700) recommends PKCE for server apps too, because it also helps stop a stolen code being slipped into someone else's sign-in.

4. The app checks who you are

The reply has an ID token, a signed JWT. Before trusting it, the server checks:

CheckFieldWhy
Signed by Google?signaturenobody changed or invented it
From Google?issmust be https://accounts.google.com or accounts.google.com
Meant for this app?audmust be your client ID, so another app's token isn't accepted
Still valid?expit expires

Use a library for this. And save the user by sub, Google's permanent ID for that person. Google's documentation warns not to use the email as the unique ID.

5. Staying signed in

Two different things expire, and mixing them up causes a lot of confusion:

  • Your app's own session (usually a cookie) decides how long the person stays signed in to your app. You choose that.
  • Google's access token lets you call Google APIs, and often lasts about an hour (read expires_in). If you only need sign-in, you may not need it at all.

To call Google APIs later, ask for access_type=offline and store the refresh token safely on the server. It can stop working: the user can remove access, for example, so be ready to ask them to sign in again.

Reading the error message

ErrorWhat it usually meansFix
redirect_uri_mismatchThe address isn't exactly one you registeredCopy the address from the error into the Console. Register local and live addresses separately
invalid_grantThe code was used twice or expired, or the refresh token no longer worksSwap each code once, straight away. Or ask the user to sign in again
invalid_clientWrong client ID or secretCheck which project's keys this environment uses
access_deniedThe user pressed Cancel, or isn't allowed (such as a non-test user while the app is in testing)Show a friendly message. Add test users or publish the app
No refresh_token in the replyYou didn't ask for offline access, or the user approved earlierUse access_type=offline, and prompt=consent when you need a new one

Mistakes to avoid

  • Client secret in front-end code. Anyone can open the page source and read it.
  • Skipping state. Your sign-in can be forged.
  • Trusting an ID token without checking aud. A token made for a different app would work on yours.
  • Using email as the user's ID. Emails change. sub doesn't.
  • Asking for more scopes than you need. People say no, and Google may require a review of your app.

Key takeaways

  • The app never sees your Google password.
  • The browser carries a one-time code. The server swaps it for tokens.
  • The ID token says who you are. The access token says what the app may do.
  • The redirect address must match exactly.
  • Check state, the signature, iss, aud and exp, and save users by sub.

Quiz: test yourself

1. Does the app ever see your Google password?

No. You type it on Google's own page.

2. Is https://myapp.com/callback/ the same as https://myapp.com/callback?

No. The last slash makes it a different redirect address, so you'd get redirect_uri_mismatch.

3. Why not send the tokens straight to the browser?

Things in an address bar can leak. A leaked code can't be swapped for tokens without the secret (or the PKCE value), so the tokens are fetched server to server.

4. Which token tells you who signed in?

The ID token. The access token is for calling APIs.

5. Which field should you save as the user's ID?

sub. It never changes, even if the email does.

Further reading

Related: Why CORS Errors Happen and What Is an API?

🔐 For everyone: see and remove apps connected to your Google account (2-minute check).

Google is a trademark of Google LLC. This guide is independent and not affiliated with Google.

Two new visual guides every week

Plus animated tech explainers on YouTube and Facebook.

More from the Library