How I built OAuth social login for WordPress — state, PKCE, and popup flows
When I built Muzzamil Two-Step Login Security, the authenticator-app side was straightforward — TOTP is a well-specified RFC. The hard part was social login: getting Google, Microsoft, and Facebook OAuth to work reliably
When I built Muzzamil Two-Step Login Security, the authenticator-app side was straightforward — TOTP is a well-specified RFC. The hard part was social login: getting Google, Microsoft, and Facebook OAuth to work reliably inside WordPress's login flow without breaking the admin experience.
Here's what actually mattered.
The core problem: WordPress login isn't an SPA
Modern OAuth tutorials assume a JavaScript SPA where you control the whole window. WordPress is server-rendered PHP — wp-login.php is a form POST, and admin-ajax.php handles fragments. Redirecting the whole browser to Google and back loses context, breaks redirect_to params, and confuses users who land on the dashboard inside an OAuth popup.
The flow I ended up with:
User clicks "Continue with Google"
→ window.open() launches OAuth in a popup
→ popup completes provider auth → hits our callback
→ callback validates state + PKCE, creates/links account
→ popup shows congratulations + countdown
→ BroadcastChannel notifies opener tab
→ popup closes, opener redirects to wp-admin
State validation is not optional
Every OAuth implementation needs a state parameter to prevent CSRF/login CSRF attacks. I generate it with random_bytes(), store it in a transient and a browser-bound HttpOnly cookie, then verify both on callback — the cookie binding stops an attacker who steals a state value from replaying it in a different browser.
State is single-use: deleted immediately after validation. Replay attempts fail closed.
PKCE for public clients
WordPress plugins are public clients — no client secret can be kept safe in distributed PHP code. PKCE (Proof Key for Code Exchange) fixes this: generate a verifier, send its SHA-256 challenge to the provider, and prove possession of the verifier on token exchange. Even if an authorization code leaks, it's useless without the verifier.
The popup problem nobody warns you about
window.opener is unreliable — browsers sever it for cross-origin navigations and privacy extensions. My first version tried window.opener.location.href = target and silently failed for a chunk of users.
The fix: generate a random 32-hex channel token in the opener, persist it in sessionStorage, pass it through the OAuth URL as zfa_ch, store it in the flow state, and have the popup broadcast {channel, destination} over BroadcastChannel. The opener listens and navigates itself. window.opener remains as a fallback.
This is the detail I'm most proud of — it makes the flow work across Chrome, Firefox, Safari, and privacy-hardened browsers.
Provider quirks worth knowing
- Google: strict redirect URI matching — every domain needs exact registration in Google Cloud Console
-
Microsoft:
commonvsorganizationstenant endpoints behave differently for personal accounts -
Facebook: requires app review for some permissions;
statehandling is pickier about encoding
What I'd do differently
Abstract the provider layer behind a single interface earlier. I started with per-provider copies and ended up refactoring — the state/PKCE/cookie logic is identical across all three, only endpoints and userinfo parsing differ.
Try it
The plugin is free on wordpress.org — source on GitHub if you want to see the actual implementation (it's GPL-2.0, steal the patterns).
Questions about the OAuth flow? Ask below — I built it, I can explain any part.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.