Kill the localhost redirect

October 1, 2026  ·  oauthcli
"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:

  1. Your CLI starts a tiny throwaway web server on 127.0.0.1, on whatever port happens to be free.
  2. It opens the user's browser at the provider's login page, with redirect_uri=http://127.0.0.1:PORT/callback.
  3. The user logs in and hits "Authorize".
  4. The provider redirects the browser to http://127.0.0.1:PORT/callback?code=... — which lands on your little server.
  5. 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.
Diagram 1 — the redirect-to-localhost flow. Five moving parts and a temp web server, just to sign in.

Why Dax wants it dead

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:

  1. Your CLI asks the provider for a device code. Back comes a user code (WDJB-MJHT), a verification URL, and a polling interval.
  2. The CLI prints: "Go to provider.com/device and enter WDJB-MJHT."
  3. The user does that on any device — phone, tablet, laptop. Log in, approve.
  4. Meanwhile the CLI polls the token endpoint every few seconds: approved yet? approved yet?
  5. 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.
Diagram 2 — the device code flow. The CLI polls; the human approves wherever it's convenient.

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.