Library › Developer Pain
🔑 Developer Pain
OAuth Explained: How “Sign in With Google” Works

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
| Name | Answers | You 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
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.
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:
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:
| Check | Field | Why |
|---|---|---|
| Signed by Google? | signature | nobody changed or invented it |
| From Google? | iss | must be https://accounts.google.com or accounts.google.com |
| Meant for this app? | aud | must be your client ID, so another app's token isn't accepted |
| Still valid? | exp | it 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
| Error | What it usually means | Fix |
|---|---|---|
redirect_uri_mismatch | The address isn't exactly one you registered | Copy the address from the error into the Console. Register local and live addresses separately |
invalid_grant | The code was used twice or expired, or the refresh token no longer works | Swap each code once, straight away. Or ask the user to sign in again |
invalid_client | Wrong client ID or secret | Check which project's keys this environment uses |
access_denied | The 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 reply | You didn't ask for offline access, or the user approved earlier | Use 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.
subdoesn'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,audandexp, and save users bysub.
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
- Google: OpenID Connect
- Google: OAuth 2.0 for web server apps
- RFC 9700: OAuth 2.0 security best current practice
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

Why CORS Errors Happen
The 'blocked by CORS policy' error explained with moving pictures: what an origin is, why browsers block, preflight requests and the server fixes that work.
Read with pictures →
What Is an API?
APIs explained like a restaurant waiter: see requests and responses travel, learn GET, POST, status codes and JSON, then make a real API call yourself.
Read with pictures →
How DNS Works
How does typing a name like resinfo.in find the right computer? Watch a DNS lookup travel from your browser to the root, .in and name servers, then try a live lookup yourself.
Read with pictures →