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

feat(auth): add account switcher to the user menu #44

Merged
pat-s merged 4 commits from t3code/switch-accounts into v15.0/forgejo-codefloe 2026-07-12 12:33:44 +00:00
Owner

Implements the account switching feature request: let a signed-in user authenticate additional accounts (e.g. a bot account) and toggle between them from the user dropdown without a full logout/login cycle.

How it works

  • The set of accounts a browser session may switch between is tracked server-side in the session (switch_accounts), so it cannot be forged by the client; a switch is only ever permitted to an account previously authenticated in that session.
  • Add an account sets a transient switch_add flag and redirects to the normal sign-in flow. The flag lets the still-signed-in user reach the login/2FA routes (which normally require being signed out) and is preserved across the several session regenerations of the sign-in flow, including 2FA.
  • Switching regenerates the session (prevents fixation) and swaps the active uid, keeping the account list.
  • Removing drops another account from the switcher. Signing out still signs out of all accounts on the device.

Changes

  • routers/web/auth/account_switch.go (new) — session helpers, add/switch/remove handlers, and the navbar middleware.
  • routers/web/auth/auth.go — preserve the switcher keys across session regeneration; record the account into the switcher on sign-in; show the login form in add-account mode.
  • routers/web/web.go — routes under /user/account_switch/*, reqSignOut bypass while adding an account, navbar middleware registration.
  • templates/base/head_navbar.tmpl + head_navbar_switch_accounts.tmpl (new) — dropdown UI; not shown in the restricted HideNavbarLinks state.
  • templates/user/auth/signin_inner.tmpl — hint shown while adding an account.
  • options/locale/locale_en-US.ini — new strings.
  • tests/integration/account_switch_test.go (new) — end-to-end tests for the plain flow and the 2FA add-account path.

Tests

TestAccountSwitch and TestAccountSwitchWith2FA pass; no regressions in the auth-adjacent integration tests.

Implements the [account switching feature request](https://forum.codefloe.com/t/feature-switch-accounts/123): let a signed-in user authenticate additional accounts (e.g. a bot account) and toggle between them from the user dropdown without a full logout/login cycle. ## How it works - The set of accounts a browser session may switch between is tracked **server-side in the session** (`switch_accounts`), so it cannot be forged by the client; a switch is only ever permitted to an account previously authenticated in that session. - **Add an account** sets a transient `switch_add` flag and redirects to the normal sign-in flow. The flag lets the still-signed-in user reach the login/2FA routes (which normally require being signed out) and is preserved across the several session regenerations of the sign-in flow, including 2FA. - **Switching** regenerates the session (prevents fixation) and swaps the active `uid`, keeping the account list. - **Removing** drops another account from the switcher. Signing out still signs out of all accounts on the device. ## Changes - `routers/web/auth/account_switch.go` (new) — session helpers, add/switch/remove handlers, and the navbar middleware. - `routers/web/auth/auth.go` — preserve the switcher keys across session regeneration; record the account into the switcher on sign-in; show the login form in add-account mode. - `routers/web/web.go` — routes under `/user/account_switch/*`, `reqSignOut` bypass while adding an account, navbar middleware registration. - `templates/base/head_navbar.tmpl` + `head_navbar_switch_accounts.tmpl` (new) — dropdown UI; not shown in the restricted `HideNavbarLinks` state. - `templates/user/auth/signin_inner.tmpl` — hint shown while adding an account. - `options/locale/locale_en-US.ini` — new strings. - `tests/integration/account_switch_test.go` (new) — end-to-end tests for the plain flow and the 2FA add-account path. ## Tests `TestAccountSwitch` and `TestAccountSwitchWith2FA` pass; no regressions in the auth-adjacent integration tests.
feat(auth): add account switcher to the user menu
All checks were successful
testing / frontend-checks (pull_request) Successful in 2m11s
testing / test-unit (pull_request) Successful in 11m56s
testing / backend-checks (pull_request) Successful in 18m2s
testing / test-remote-cacher (redis) (pull_request) Successful in 5m28s
testing / test-e2e (pull_request) Successful in 28m37s
testing / test-sqlite (pull_request) Successful in 45m13s
testing / test-remote-cacher (garnet) (pull_request) Successful in 3m27s
testing / test-mysql (pull_request) Successful in 33m51s
testing / test-remote-cacher (valkey) (pull_request) Successful in 5m24s
testing / test-pgsql (pull_request) Successful in 51m28s
84098af7a8
Allow a signed-in user to authenticate additional accounts and switch
between them from the user dropdown without a full logout/login cycle.

The set of switchable accounts is tracked server-side in the session and
survives the session regenerations of the sign-in flow (including 2FA),
so a switch is only ever permitted to an account previously authenticated
in the same session. Signing out still signs out of all accounts on the
device.
fix(auth): carry account-switch add intent in the URL
All checks were successful
testing / frontend-checks (pull_request) Successful in 1m57s
testing / backend-checks (pull_request) Successful in 14m30s
testing / test-unit (pull_request) Successful in 18m14s
testing / test-remote-cacher (valkey) (pull_request) Successful in 3m14s
testing / test-remote-cacher (garnet) (pull_request) Successful in 3m19s
testing / test-remote-cacher (redis) (pull_request) Successful in 5m30s
testing / test-sqlite (pull_request) Successful in 29m53s
testing / test-mysql (pull_request) Successful in 30m17s
testing / test-e2e (pull_request) Successful in 31m20s
testing / test-pgsql (pull_request) Successful in 42m32s
58cddabbaf
The "Add an account" flow relied solely on a session flag persisting
between the POST that starts it and the follow-up GET of the login page.
When that write does not survive the redirect (observed with some session
backends and multi-node deployments), the login page redirected the
still-signed-in user away and the flow silently did nothing.

Carry the intent in an "add_account" query parameter through the login
form instead, so the login page enters add mode independently of the
session. Only the URL query is inspected, so it is safe to read from the
sign-out middleware without consuming the request body. The session flag
is kept as a best-effort signal for the 2FA continuation, which has no
query parameter.

Add an integration test covering the query-only path (no session flag)
and one replaying the exact browser redirect sequence.
feat(auth): clarify the account switcher in the user menu
Some checks failed
testing / frontend-checks (pull_request) Successful in 2m25s
testing / test-remote-cacher (garnet) (pull_request) Successful in 5m0s
testing / test-remote-cacher (redis) (pull_request) Successful in 5m24s
testing / backend-checks (pull_request) Successful in 15m21s
testing / test-unit (pull_request) Successful in 18m23s
testing / test-remote-cacher (valkey) (pull_request) Successful in 2m45s
testing / test-mysql (pull_request) Successful in 31m49s
testing / test-sqlite (pull_request) Successful in 29m18s
testing / test-e2e (pull_request) Successful in 31m33s
testing / test-pgsql (pull_request) Has been cancelled
5aaf934402
Label the switch targets with a "Switch account" section header, prefix
each with a switch icon, and add a "Switch to <name>" tooltip so the
entries read clearly as an account-switching action rather than ordinary
menu links.
fix(auth): vertically center the account-switcher remove button
All checks were successful
testing / test-remote-cacher (garnet) (pull_request) Successful in 5m29s
testing / frontend-checks (pull_request) Successful in 2m2s
testing / backend-checks (pull_request) Successful in 15m5s
testing / test-unit (pull_request) Successful in 18m21s
testing / test-remote-cacher (valkey) (pull_request) Successful in 3m9s
testing / test-e2e (pull_request) Successful in 30m45s
testing / test-remote-cacher (redis) (pull_request) Successful in 3m11s
testing / test-sqlite (pull_request) Successful in 29m50s
testing / test-mysql (pull_request) Successful in 32m21s
testing / test-pgsql (pull_request) Successful in 45m40s
milestone / set (pull_request_target) Has been skipped
issue-labels / backporting (pull_request_target) Has been skipped
5a0317af5d
pat-s changed title from WIP: feat(auth): add account switcher to the user menu to feat(auth): add account switcher to the user menu 2026-07-12 12:33:18 +00:00
pat-s merged commit da879fef73 into v15.0/forgejo-codefloe 2026-07-12 12:33:44 +00:00
pat-s deleted branch t3code/switch-accounts 2026-07-12 12:33:45 +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!44
No description provided.