Social login with OAuth
Add Sign in with Google to Taskboard by running the authorization code flow with PKCE and state, then turning the provider's answer into your own session.
Social login with OAuth
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Social login hands the proof of identity to a provider such as Google, but your app still creates and owns its own session afterwards.
- The authorization code flow sends the browser to the provider, gets a short-lived code back in the redirect, and exchanges that code for tokens on the server where the client secret lives.
- The state parameter is a random value you store before the redirect and compare on return, which stops an attacker from completing a login you did not start.
- PKCE sends a hash of a secret verifier with the request and the verifier itself with the code exchange, so a stolen code is useless without it.
- OAuth by itself grants access to resources and OpenID Connect adds the identity layer, so for login you read the verified ID token or the userinfo endpoint, not just any access token.
Why delegate sign-in
Every password you store is a liability: you must hash it, rate-limit guesses, handle resets, and live with the breach if one happens. Users, meanwhile, already have an account with a provider that does all of that well. Social login says: “let Google vouch for this person, and I will trust the answer.”
The mechanism is OAuth 2.0, with OpenID Connect (OIDC) on top for identity. OAuth alone is about access: “this app may read this user’s calendar.” OIDC adds a standard way to say who the user is. For “Sign in with Google” you use both, as one flow. OAuth2 and OIDC in practice covers the protocol in depth; here is how it lands in Taskboard.
The four steps
Think of it like a visa. You do not give a border guard your home government’s secret; the government issues a stamped document, and the guard checks the stamp. Concretely, the authorization code flow is:
- Redirect out. Taskboard sends the browser to Google’s authorize URL with a
client_id, aredirect_uri, ascope, a randomstateand a PKCEcode_challenge. - User consents at Google. Your app never sees their Google password.
- Redirect back. Google sends the browser to
https://taskboard.example/auth/callback?code=…&state=…. - Exchange on the server. Taskboard’s server posts the
code, theclient_secretand the PKCEcode_verifierto Google’s token endpoint, and receives an ID token (and an access token). It reads the verified identity from the ID token.
Then Taskboard does the same thing it does after a password login: find or create the user, create a session row, set the sid cookie. From that moment the rest of the app cannot tell how the person signed in. That is the design goal, and it is why the sign-up and login flow and this one share a back half.
Why a code and not the tokens directly
The code arrives in a URL, which means it lands in browser history and logs, and passes through the browser. A code is deliberately useless by itself: it is short-lived, single-use, and can only be exchanged by someone with the client_secret. The valuable tokens are fetched in a direct server-to-server call. That is why the older “implicit” flow, which put tokens in the URL, has been dropped from current OAuth guidance (from my knowledge of the OAuth security guidance; I did not re-read the document while writing this).
State and PKCE
Two small values do most of the security work.
state is a random string you create before redirecting and store (in a short-lived cookie or the session). When the callback arrives, compare it to the stored one. Without it, an attacker can start a login in their own account, take the callback URL, and trick your browser into visiting it, signing you in as the attacker. Anything you then save goes into their account.
PKCE (Proof Key for Code Exchange, pronounced “pixie”) binds the code to the browser that started the flow. You generate a random code_verifier, send only its SHA-256 hash (code_challenge) at the start, and send the verifier itself at the exchange. If an attacker steals the code in transit, they cannot complete the exchange without the verifier.
I computed the challenge with Node’s crypto module (v26.13.1). The RFC 7636 appendix gives a test vector: verifier dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk should hash to E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM, and my code printed exactly that:
import { createHash, randomBytes } from "node:crypto";
const challenge = (verifier: string) =>
createHash("sha256").update(verifier).digest("base64url");
const verifier = randomBytes(32).toString("base64url"); // 43 chars
const state = randomBytes(16).toString("base64url");
and the authorize URL is built with URLSearchParams:
const url = new URL("https://accounts.example.com/authorize");
url.search = new URLSearchParams({
response_type: "code",
client_id: "taskboard",
redirect_uri: "http://localhost:8787/auth/callback",
scope: "openid email profile",
state,
code_challenge: challenge(verifier),
code_challenge_method: "S256",
}).toString();
I did not run this against a real provider; that needs a registered application and its credentials. The URL parameters are the standard ones from the OAuth and OIDC specifications. PKCE was first invented for apps that cannot keep a secret, and it is now recommended even for server-side apps like this one.
The callback handler
On the server, the callback does four checks in a fixed order:
// 1. state matches what we stored, otherwise reject
// 2. POST code + code_verifier + client_secret to the token endpoint
// 3. verify the ID token: signature, issuer, audience == our client_id, not expired
// 4. find-or-create the user by (provider, subject) and issue our own session
Step 3 is the one people skip. An ID token is a signed JWT, and you must check that it was issued by the right provider for your app (aud), otherwise a token issued to another app could log someone in to yours. Use a maintained OIDC library for this rather than hand-parsing, and see authentication in a Node API for libraries.
For step 4, key the account on the provider’s stable subject id (sub), not the email. Emails change, and an unverified email from a provider is not proof of ownership. Linking a social login to an existing password account by email is a classic account-takeover route unless the provider marks the email as verified, so decide that rule on purpose.
Where the pieces sit in the stack
| Layer | Responsibility |
|---|---|
| Browser | Go to /auth/google/start and later land on the callback; never holds the client secret |
| API | Builds the redirect, stores state and verifier, exchanges the code, verifies the token, issues the session |
| Database | Stores (provider, sub) linked to a user row, plus the session |
| Config | client_id, client_secret, redirect URIs per environment |
The redirect URI is an exact-match setting registered with the provider, so local, staging and production each need their own entry. That is one more reason the environment variables and secrets lesson matters.
What the video covers
ByteByteGo’s “OAuth 2 Explained In Simple Terms” is about four and a half minutes, published June 2023. Its description is the channel’s boilerplate and lists no chapters, and I could not read a transcript, so I will not describe its contents beyond the title. It is a short conceptual introduction, not an implementation guide. The PKCE, state and callback details here are this lesson’s own, drawn from the specs. OktaDev’s “An Illustrated Guide to OAuth and OpenID Connect” is linked for a longer treatment; it dates from 2019, and the concepts hold though some recommendations have moved on.
Where this fits
After sign-in, the question is no longer who you are but what you may touch. That decision has to be made in two places and only one of them is the real one. Next: authorization in the API and the UI.
Check yourself
Why does the code exchange happen on the server and not in the browser?
The exchange needs the client secret, which must never ship to a browser, and doing it server side keeps the tokens out of the page's JavaScript.
What attack does the state parameter prevent?
A login CSRF, where an attacker gets your browser to finish a login flow the attacker started, which would sign you in as the attacker's account.
What does PKCE protect against?
An attacker who intercepts the authorization code, since without the original verifier the provider will not honour the code exchange.
After the provider confirms the user, what does Taskboard issue?
Its own session cookie, the same one a password login creates, so the rest of the app does not care how the person signed in.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.