"Continue with Google" is on almost every website now. Behind that button is OAuth 2.0 — a standard that lets users sign in with an account they already have, without ever giving your app their password. If you are building anything with users, you need to understand it.
The problem OAuth solves
Before OAuth, an app that wanted your Google contacts needed your Google password. That meant the app could read everything, and if it got hacked, your password leaked. OAuth fixes this: the user approves access on Google's own page, and your app gets a limited token instead of a password.
The authorization code flow
This is the flow you will use in a web app. It has four steps:
- Your app redirects the user to Google with your client ID, the permissions you want (called scopes, like "email" or "profile"), and a redirect URL.
- The user signs in on Google and approves. Google never shows your app the password.
- Google redirects back to your app with a code. This code is short-lived and single-use.
- Your server exchanges the code for tokens — an access token for API calls and a refresh token to get new ones later.
// Step 1: send the user to Google
const authUrl = new URL("https://accounts.google.com/o/oauth2/v2/auth");
authUrl.searchParams.set("client_id", YOUR_CLIENT_ID);
authUrl.searchParams.set("redirect_uri", "https://yourapp.com/auth/callback");
authUrl.searchParams.set("response_type", "code");
authUrl.searchParams.set("scope", "openid email profile");
authUrl.searchParams.set("state", randomString); // prevents CSRF attacks
window.location.href = authUrl.toString();
Access tokens vs ID tokens
An access token lets your app call an API on the user's behalf — "read this user's email". An ID token (from OpenID Connect, which sits on top of OAuth) proves who the user is — "this user is arhan@example.com". For sign-in, you want the ID token. For API access, you want the access token. Mixing them up is a classic beginner mistake.
Scopes: ask for as little as possible
Scopes decide what your app can do. email and profile are enough for sign-in. Asking for full mailbox access when you only need an email address is how apps lose user trust — and it is also a security risk if your tokens ever leak. Request extra scopes only when the user actually uses that feature.
Common mistakes
- Storing tokens in localStorage. Any JavaScript on the page can read it. Use httpOnly cookies for sessions, and keep tokens server-side.
-
Skipping the state parameter. Without it, an attacker can trick a user into logging in as the attacker. Always generate a random
state, store it, and check it on the callback. - Using the implicit flow. The old flow that returned tokens directly in the URL is deprecated. Use the authorization code flow with PKCE, especially for mobile and single-page apps.
When to use OAuth
Use it when users already have a Google, GitHub, or Microsoft account — which is almost always. Do not build your own password system unless you have a reason: storing passwords safely means hashing, salting, reset flows, and breach monitoring. OAuth hands that job to companies that do it full-time.
Top comments (0)