"i'm begging you once again can we please kill the redirect to localhost oauth flow
it's so bad, such bad ux so brittle in so many situations
the client can poll for a code please please please"
— Dax (@thdxr), building the opencode agent
127K views and 2.3K likes in a day. He's not wrong. Here's the full story: what the flow is, why it's terrible, and the better flow that's been sitting there since 2019.
The flow everyone's copy-pasting
You build a CLI. It needs the user to sign in with some OAuth provider. The standard recipe, straight out of RFC 8252 ("OAuth 2.0 for Native Apps"), goes like this:
- Your CLI starts a tiny throwaway web server on
127.0.0.1, on whatever port happens to be free. - It opens the user's browser at the provider's login page, with
redirect_uri=http://127.0.0.1:PORT/callback. - The user logs in and hits "Authorize".
- The provider redirects the browser to
http://127.0.0.1:PORT/callback?code=...— which lands on your little server. - You grab the code from the query string, trade it for tokens, and kill the server.
The browser is just a courier, ferrying the authorization code from the provider back to an HTTP server running inside your terminal app. Cute trick. It works fine on a laptop.
sequenceDiagram
participant CLI as CLI app
participant Srv as 127.0.0.1:54321
(temp server)
participant Browser as Browser
participant IdP as OAuth provider
CLI->>Srv: start temp HTTP server on a free port
CLI->>Browser: open login page
redirect_uri=http://127.0.0.1:54321/callback
Browser->>IdP: GET /authorize
IdP-->>Browser: login page
Browser->>IdP: user logs in + approves
IdP-->>Browser: 302 → http://127.0.0.1:54321/callback?code=abc
Browser->>Srv: GET /callback?code=abc
Srv-->>CLI: hand over the code
CLI->>IdP: POST /token (code → tokens)
IdP-->>CLI: access + refresh token
CLI->>Srv: shut down temp server
Note over Browser,Srv: Over SSH this breaks: the redirect targets
the remote machine's loopback, which your
local browser cannot reach.
Why Dax wants it dead
- "localhost on which machine?" — SSH into a remote box, run the CLI there, and the redirect targets the remote machine's loopback. Your laptop's browser can't reach it. Dead on arrival for remote dev, containers, and headless servers. This was literally one of the top replies to the post.
- Port roulette. You have to grab a free port at runtime and hope the provider accepts it in the redirect URI.
- Firewalls and sandboxed browsers can block the loopback listener entirely.
- Browsers are getting stricter about public HTTPS pages bouncing down to
http://localhost(Private Network Access protections). - Race conditions. The temp server must stay alive until the exact moment the redirect lands. Quit a beat early and the user gets "this site can't be reached."
- Cargo-culting. As another reply put it: "it was in the pretrain so every llm just copies it." Every new CLI agent reimplements the same dance because it's what all the sample code shows.
The fix: let the client poll
Dax's plea — "the client can poll for a code" — is the Device Authorization Grant, RFC 8628. It's from 2019. It's not new, just underused:
- Your CLI asks the provider for a device code. Back comes a user code (
WDJB-MJHT), a verification URL, and a polling interval. - The CLI prints: "Go to provider.com/device and enter WDJB-MJHT."
- The user does that on any device — phone, tablet, laptop. Log in, approve.
- Meanwhile the CLI polls the token endpoint every few seconds: approved yet? approved yet?
- The moment you approve, the next poll returns real tokens. Done.
sequenceDiagram
participant CLI as CLI app
participant IdP as OAuth provider
participant Phone as Your phone
(any browser)
CLI->>IdP: POST /device/authorize
IdP-->>CLI: device_code, user_code WDJB-MJHT,
verification_uri, interval = 5s
Note over CLI: print "go to provider.com/device
and enter WDJB-MJHT"
loop every 5 seconds
CLI->>IdP: POST /token (device_code)
IdP-->>CLI: authorization_pending…
end
Phone->>IdP: open verification_uri, enter code
IdP-->>Phone: login + approve screen
Phone->>IdP: approve ✅
CLI->>IdP: POST /token (device_code)
IdP-->>CLI: access + refresh token 🎉
Note over CLI,Phone: No localhost server. No ports.
Works over SSH and on headless boxes —
approval can happen on your phone.
Verdict: Dax is right. The localhost redirect was designed for desktop apps in 2017; for CLIs and coding agents, device-code polling is strictly better UX. The only reason the redirect persists is sample code. Kill it.
P.S. — this is exactly how codex login --device-auth signed in my own assistant's Codex CLI: a code on screen, a tap on the phone, tokens in the terminal. Zero localhost involved.