OAuth 2.0 Explained: How Sign In With Google Works

OAuth 2.0 explained in plain terms starts with a familiar moment: you click Sign in with Google on a new app and, seconds later, you are inside without ever typing your password into that app. The central question is how an app can act on your behalf without ever seeing your password, and the answer is delegated access: Google checks who you are, then hands the app a limited token that says what it may do, and nothing more.

OAuth 2.0 is an authorisation framework, not a login system. It does not prove who you are; it lets you grant a third party limited permission to reach something you own, such as your calendar or photos, without handing over the keys to your whole account.

It is why a printing service can read one album, or a calendar app see your free hours, while neither learns your password or reaches your email. This article explains what OAuth 2.0 is, how its flows work, and where people most often get it wrong.

Key takeaways

  • OAuth 2.0 grants limited, delegated access through tokens, never by sharing passwords.
  • An access token says what an app may do, and a refresh token lets it ask for a new one.
  • Scopes limit a token to specific actions, such as reading a calendar but not sending mail.
  • The authorisation code flow with PKCE is the safe default for modern apps.
  • Tokens must be short-lived, stored carefully, and revoked when they are no longer needed.

What OAuth 2.0 is

OAuth 2.0 is a standard, published as RFC 6749 in 2012, that describes how a user can let one service reach resources held by another service. You are the resource owner, the app asking for access is the client, the service holding your data runs the authorisation server and the resource server, and the token is the proof that permission was granted.

The key idea is delegation. Instead of giving your password to every app that wants to help you, you log in once with the service that holds your data, approve a specific request, and the app receives a token that carries only the permissions you approved. Your password never leaves the service you trust, and the app never needs to store it.

The Sign in with Google button works this way too: the app asks Google to vouch for you and hand over a token for your name and email address. Our guide to SAML explains an older enterprise approach to the same problem, and our guide to Kerberos explains how company networks have handled delegated trust for decades.

How the authorisation code flow works

The authorisation code flow is the workhorse of OAuth 2.0, and it runs in four clear steps. First, the app sends your browser to the authorisation server with a request naming itself, the permissions it wants, and a return address. Second, you log in there and approve or refuse the request, dealing only with the service you already trust.

Third, the authorisation server sends your browser back to the app carrying a short-lived authorisation code. Fourth, the app exchanges that code, along with its own credentials, directly with the authorisation server for an access token and a refresh token. The code itself is useless to anyone who intercepts it in the browser, because it can only be redeemed by the app, from its own server, within minutes.

Modern apps add PKCE, pronounced pixie, to this flow. The app creates a random secret before the first step, sends only a hash of it, and then presents the original secret when redeeming the code. Even if someone steals the authorisation code, they cannot redeem it without the secret, which closes the main hole in the flow for mobile and single-page apps.

The browser carries the user through approval, while the valuable token exchange happens server to server, out of sight, which is why this flow is the default recommendation today.

Close-up photograph of hands holding a smartphone showing a login approval screen, a laptop keyboard blurred in the background
Close-up photograph of hands holding a smartphone showing a login approval screen, a laptop keyboard blurred in the background, showing the approval moment at the heart of OAuth 2.0.

Access tokens, refresh tokens, and scopes

An access token is the thing the app actually uses. It is usually a random string that means nothing on its own; the resource server looks it up, or checks its signature, and then decides what the bearer may do. Access tokens are deliberately short-lived, often an hour or less, so a stolen one quickly becomes useless.

A refresh token is the longer-lived companion that lets the app get a fresh access token without bothering you again. It is stored only on the app’s server, never in the browser, and it can be revoked at any time, which immediately cuts off the app’s access. If a refresh token is stolen, the damage can be serious, which is why good providers rotate them and watch for reuse.

Scopes are the fine print of the permission. A token might allow reading your calendar but not creating events, or reading your contacts but not your mail. The authorisation screen shows you the scopes before you approve, and a careful user reads them, because a token with broad scopes is nearly as powerful as the password it replaced.

Tokens also carry an audience and an expiry. The audience says which service the token is for, so a calendar token cannot be presented to a mail server, and the expiry limits how long a leak stays dangerous.

Comparing the OAuth 2.0 flows

OAuth 2.0 defines several flows, called grant types, and choosing the wrong one is the most common mistake teams make. The authorisation code flow suits apps with users and a server; the client credentials flow suits services talking to each other with no user involved; and the device flow suits televisions and printers that have no keyboard at all.

The table below compares the flows teams actually meet in practice.

Aspect Authorisation code with PKCE Client credentials Device flow
Who is involved A user approving in a browser Two services, no user A user on a second device
Best for Web, mobile, and single-page apps Server-to-server calls TVs, printers, and consoles
Refresh token Yes, stored server-side No, a new token each time Yes, after the user approves
Main risk Code interception without PKCE Stolen client secret Slow approval or phishing
Use it when Almost always, for user access Back-end jobs and APIs The device has no keyboard

The implicit flow, which once returned tokens directly to the browser, is now deprecated, because tokens in an address bar leak through history and logs. Moving to the authorisation code flow with PKCE is the most valuable upgrade an old system can make.

Where OAuth 2.0 is used

Social login is the most visible use. Sign in with Google, Apple, or GitHub are all OAuth 2.0 under the surface, and they save users from creating yet another password while giving the app a verified email address and a name. Our guide to passkeys explains a newer approach that removes passwords entirely, and it pairs well with OAuth rather than replacing it.

Third-party integrations are the second great use. A project tool that reads your calendar, a photo app that imports from cloud storage, or a finance app that reads bank transactions all rely on OAuth 2.0 scopes to keep each connection narrow. Revoking one integration in your account settings cuts off exactly that app, and nothing else.

Microservices use the client credentials flow to call each other safely. Each service holds its own credentials, requests a token for the narrow scope it needs, and presents it with every call, so no service trusts another just because it sits on the same network.

Common mistakes and how to avoid them

The most common mistake is treating an access token like a password and storing it where it does not belong. Tokens in browser local storage can be read by any script that slips into the page, and tokens in URLs leak into logs and analytics. Keep tokens server-side where you can, and keep browser tokens short-lived.

The second mistake is requesting scopes far broader than the app needs. An app that only reads your profile should not ask for full mail access, and users notice: broad requests are refused more often, and a breach of a broad token is far worse. Ask for the minimum, and request more only when the user reaches the feature that needs it.

The third mistake is skipping PKCE on mobile and single-page apps, on the assumption that the redirect is safe. It is not: another app on the phone can claim the redirect, and a stolen code without PKCE is redeemable. PKCE costs a few lines of code and removes the risk entirely.

The fourth is never expiring refresh tokens. A refresh token that lives forever is a password by another name, so rotate them on use and revoke the whole family if one is reused.

OAuth 2.0 checklist

  • Use the authorisation code flow with PKCE for every app with a user.
  • Request the narrowest scopes that let the feature work, and nothing more.
  • Keep access tokens short-lived and refresh tokens server-side only.
  • Never put tokens in URLs, logs, or browser local storage.
  • Validate the redirect address exactly, with no wildcards or open patterns.
  • Rotate refresh tokens on use and revoke the family if one is reused.
  • Check the token audience and expiry before trusting it on every request.
  • Give users a clear settings page to review and revoke connected apps.
  • Retire the implicit flow wherever it still survives in older code.

Frequently asked questions

Is OAuth 2.0 the same as logging in?

No, it is authorisation, not authentication. It grants an app limited access to your data; the login part is usually layered on top with OpenID Connect, which adds an identity token to the same flow.

Does the app ever see my password?

No, and that is the point. You type your password only into the service that holds your data, and the app receives a token instead, which it can use for the approved actions and nothing else.

What happens if an access token is stolen?

The thief can do whatever the token’s scopes allow until it expires, which is why access tokens live for an hour or less. Revoking the refresh token cuts off the supply of new access tokens immediately.

What is PKCE and do I need it?

PKCE is a proof that the app redeeming an authorisation code is the same app that requested it, made from a random secret. Every public client, including mobile and single-page apps, should use it.

Can I revoke an app’s access?

Yes. Your account settings on the provider list every connected app with its scopes, and revoking one there invalidates its tokens at once, without changing your password or affecting other apps.

Why do some apps ask for so many permissions?

Often because the developers requested a broad default scope rather than the minimum. You can refuse, and a well-built app will still work, asking for more only when you reach the feature that needs it.

The bottom line

OAuth 2.0 explained in one line is this: let people grant narrow, revocable permission without ever sharing a password.

Used well, with the authorisation code flow, PKCE, short-lived tokens, and minimal scopes, it is the reason the modern web can connect services safely. Used carelessly, with broad scopes and tokens lying around, it becomes a spare key left under the mat.

This article is general information about how OAuth 2.0 works, not advice about securing a particular application.

Latest articles

Scroll to Top