codefloe/forgejo-codefloe
Watch
4
12
Fork
You've already forked forgejo-codefloe
0

fix(auth): keep the added account when adding one with 2FA #185

Merged
pat-s merged 1 commit from fix/account-switch-2fa-remember into v16.0/forgejo-codefloe 2026-09-30 17:05:20 +00:00
Owner

Adding an account through the account switcher failed silently when the new account needed 2FA and the signed-in account had a remember-me cookie.
You stayed signed in as the previous account, and the new one never appeared in the switcher.

Cause

After the password step, the sign-in flow redirects to /user/two_factor or /user/webauthn.
Those handlers, plus /user/two_factor/scratch and /user/login/openid, call CheckAutoLogin.
The remember-me cookie still belonged to the previous account, so CheckAutoLogin signed that account back in and redirected to / before the 2FA challenge was shown.
The existing TestAccountSwitchWith2FA missed it because it signs in without "Remember me" and posts the passcode without loading the 2FA page.

What changes

  • CheckAutoLogin does nothing while an account is being added, matching what the sign-in page already did in add mode.
  • When the added account signs in without "Remember me", an existing remember-me cookie is moved to it.
    Before, an expired session signed the browser back in as the previous account.
    The navbar switcher already moves the cookie on every switch; both paths now share moveRememberCookie.
    Normal (non-add) sign-ins are unchanged.
  • README "Account switcher" row: this PR is linked.

Tests

  • TestAccountSwitchWith2FARemembered: adds a TOTP account while remembered; the 2FA, scratch and WebAuthn pages now render instead of answering 303 → /, and the added account ends up active with the previous one in the switcher.
  • TestAccountSwitchAddMovesRememberCookie: after adding an account without "Remember me", the remember-me cookie alone signs in the added account.
  • Both failed before the fix.
  • TestAccountSwitch*, TestSelectAccount*, TestLTA*, TestSignin*, TestTOTP*, TestTwoFactor*, TestWebAuthn*, TestUserLogin*, TestOAuth2*, TestAuthorize* pass on sqlite (48 tests).
Adding an account through the account switcher failed silently when the new account needed 2FA and the signed-in account had a remember-me cookie. You stayed signed in as the previous account, and the new one never appeared in the switcher. ## Cause After the password step, the sign-in flow redirects to `/user/two_factor` or `/user/webauthn`. Those handlers, plus `/user/two_factor/scratch` and `/user/login/openid`, call `CheckAutoLogin`. The remember-me cookie still belonged to the previous account, so `CheckAutoLogin` signed that account back in and redirected to `/` before the 2FA challenge was shown. The existing `TestAccountSwitchWith2FA` missed it because it signs in without "Remember me" and posts the passcode without loading the 2FA page. ## What changes - `CheckAutoLogin` does nothing while an account is being added, matching what the sign-in page already did in add mode. - When the added account signs in without "Remember me", an existing remember-me cookie is moved to it. Before, an expired session signed the browser back in as the previous account. The navbar switcher already moves the cookie on every switch; both paths now share `moveRememberCookie`. Normal (non-add) sign-ins are unchanged. - README "Account switcher" row: this PR is linked. ## Tests - `TestAccountSwitchWith2FARemembered`: adds a TOTP account while remembered; the 2FA, scratch and WebAuthn pages now render instead of answering `303 → /`, and the added account ends up active with the previous one in the switcher. - `TestAccountSwitchAddMovesRememberCookie`: after adding an account without "Remember me", the remember-me cookie alone signs in the added account. - Both failed before the fix. - `TestAccountSwitch*`, `TestSelectAccount*`, `TestLTA*`, `TestSignin*`, `TestTOTP*`, `TestTwoFactor*`, `TestWebAuthn*`, `TestUserLogin*`, `TestOAuth2*`, `TestAuthorize*` pass on sqlite (48 tests).
pat-s force-pushed fix/account-switch-2fa-remember from 204716f8d9
Some checks failed
testing / test-unit (pull_request) Has been cancelled
testing / test-e2e (pull_request) Has been cancelled
testing / test-remote-cacher (redis) (pull_request) Has been cancelled
testing / test-remote-cacher (valkey) (pull_request) Has been cancelled
testing / test-remote-cacher (garnet) (pull_request) Has been cancelled
testing / test-mysql (pull_request) Has been cancelled
testing / test-pgsql (pull_request) Has been cancelled
testing / test-sqlite (pull_request) Has been cancelled
testing / frontend-checks (pull_request) Has been cancelled
testing / backend-checks (pull_request) Has been cancelled
to 4ccd45a56e
All checks were successful
testing / frontend-checks (pull_request) Successful in 1m23s
testing / backend-checks (pull_request) Successful in 5m6s
testing / test-unit (pull_request) Successful in 10m33s
testing / test-remote-cacher (garnet) (pull_request) Successful in 3m19s
testing / test-remote-cacher (valkey) (pull_request) Successful in 3m20s
testing / test-remote-cacher (redis) (pull_request) Successful in 3m20s
testing / test-mysql (pull_request) Successful in 32m59s
testing / test-e2e (pull_request) Successful in 31m36s
testing / test-sqlite (pull_request) Successful in 49m19s
testing / test-pgsql (pull_request) Successful in 52m20s
issue-labels / backporting (pull_request_target) Has been skipped
milestone / set (pull_request_target) Has been skipped
2026-09-30 16:07:48 +00:00
Compare
pat-s scheduled this pull request to auto merge when all checks succeed 2026-09-30 16:20:12 +00:00
pat-s merged commit 094726d455 into v16.0/forgejo-codefloe 2026-09-30 17:05:20 +00:00
pat-s deleted branch fix/account-switch-2fa-remember 2026-09-30 17:05:21 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
codefloe/forgejo-codefloe!185
No description provided.