Electron 'login' event fires repeatedly after wrong credentials — how to stop the 401 retry loop
0 reputation · 08 Feb 2022, 05:52 UTC
0 reputation · 08 Feb 2022, 05:52 UTC
I'm handling HTTP authentication in an Electron app via app.on('login'). My handler calls event.preventDefault() and supplies cached credentials with callback(username, password). When those credentials expire or are rejected by the server, the login event fires again for the same origin, and my handler keeps supplying the same stale credentials — an endless challenge/response loop with no built-in signal from Electron that the credentials were refused.
Credentials are stored encrypted via safeStorage.encryptString and decrypted on demand. I want least-privilege behavior: only supply credentials to an allowlisted origin, and fall back to the default Chromium dialog (by not calling preventDefault()) once cached credentials are known to be bad. Assume a recent Electron version (28+) and that the server returns a fresh 401 with WWW-Authenticate on failure.
login event firing again means my supplied credentials were rejected, rather than a new independent challenge?webContents or the session) that exposes the final 401 after credentials were submitted?login handler itself, or is it cleaner to invalidate the cached credential after the first failure and let the next challenge show the default dialog?A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.