Repository navigation
Description
[Bug] codex login removes the existing auth.json before the flow can succeed, so a failed login logs the user out
Component: Codex CLI (codex login, codex login --device-auth)
Version: tested on codex-cli 0.153.4 (linux-x86_64, musl standalone build)
Summary
codex login appears to delete the existing credentials at the start of the flow
rather than writing new ones atomically on success. If the flow fails at any later step
(browser not reachable, token exchange transport error, expired device code, wrong account
in the browser, …), the user is left logged out even though they were logged in before.
This turns a recoverable, transient failure into a destructive one, and it is especially
painful on headless/remote hosts where the only way to fix it is to re-run a login flow
that just failed.
Steps to reproduce
-
Have a working login:
codex login status→Logged in using ChatGPT. -
Start a new login that will fail late, for example while the token endpoint is
temporarily unreachable:codex login --device-auth # complete the browser step # -> ERROR: Error logging in with device code: device code exchange failed: # error sending request for url (https://auth.openai.com/oauth/token)
-
Check the state:
ls ~/.codex/auth.json # No such file or directory codex login status # Not logged in
Observed twice in a live environment (a remote Linux host): the previously valid
auth.json was gone after a failed login attempt.
Expected
Either:
- the existing credentials are left untouched until the new ones are successfully
obtained (write to a temp file, then atomically rename), or - the old credentials are backed up and restored automatically when the flow fails,
or at minimum - the CLI warns that continuing will remove the current login.
Actual
The previous credentials are removed before the flow completes; a failed login means the
user must successfully complete a login (possibly under the same broken network
conditions) before Codex works again.
Impact
- On remote/headless machines, a single transient network error can take a working Codex
installation offline until an operator can re-authenticate. - Makes it risky to attempt a login (e.g. to switch accounts) while anything about the
network path is uncertain.
Suggestion
- Write credentials atomically; only replace the previous file on success.
- On failure, restore the previous credentials and say so in the error output.
- Consider printing the target
CODEX_HOMEand whether an existing login will be
replaced, sinceCODEX_HOMEmay point at a non-default directory.
Environment
codex-cli 0.153.4
Linux x86_64 (headless server), CODEX_HOME=/<user>/.codex (non-default path)
Reached OpenAI through a local HTTP proxy (HTTP_PROXY/HTTPS_PROXY set for the process)
Related issues
- [macOS 26.818.41705] Thread resume signs out desktop — rotated refresh token never persisted to auth.json, fresh login invalidated in 76s #40267 — desktop sign-out caused by refresh-token rotation not being persisted
(different mechanism: the token is invalidated, whereas here the file is removed by the
login command itself before it can succeed). - Codex Windows: fresh OAuth refresh token immediately invalidated when two ChatGPT accounts are signed in on the web #39199 — two ChatGPT accounts invalidating each other's refresh tokens
(adjacent symptom: credentials lost on a machine, but again not caused bycodex login).
Minimal fix sketch
// pseudo-code: make credential replacement atomic and non-destructive on failure
let tmp = auth_path.with_extension("json.tmp");
write(tmp, &new_credentials)?; // only after the flow has fully succeeded
fs::rename(tmp, auth_path)?; // atomic replace
// on any failure path: leave the previous auth.json untouched
Activity
trinity0506 commentedon Sep 18, 2026
Thanks — I verified this against
main(7498521d288b) and agree that the file-level sketch is the wrong boundary.Concretely:
codex-rs/cli/src/login.rs:122—clear_existing_auth_before_login()callslogout_with_revoke(...)and onlytracing::warn!s on failure (:128). It runs before the replacement flow inlogin_with_chatgpt(:147),run_login_with_device_code(:335) andrun_login_with_device_code_fallback_to_browser(:386).codex-rs/login/src/auth/manager.rs:979—logout_with_revoke()loads the stored auth, callsrevoke_auth_tokens(...)(failure only warned,:996), thenlogout_all_stores(...).So the superseded credential can be gone in both senses described above, and restoring
auth.jsonon a late failure may restore an already-revoked refresh token. Superseding the "minimal fix sketch" in the issue body with the lifecycle version:AuthCredentialsStoreMode(not as anauth.jsonrename property);Field evidence from our side (headless Linux server,
codex-cli 0.153.4): aftercodex login --device-authwas started and the flow failed late at the token exchange (error sending request for url (https://auth.openai.com/oauth/token)),auth.jsonwas already absent while the flow was still running, andcodex login statusreportedNot logged in. We did not capture whether the revoke endpoint was called, so we cannot confirm the server-side half from our logs.One adjacent asymmetry worth checking while the invariant is being moved:
run_login_with_api_key(codex-rs/cli/src/login.rs:204) does not go throughclear_existing_auth_before_loginat all — it callslogin_with_api_keydirectly and overwrites the store, i.e. the previous ChatGPT tokens are dropped locally without being revoked.