Don't show the create-account form once AuthGuard already has an account
Lint / Lint (push) Successful in 49s
Tests / Go backend (push) Successful in 46s
Tests / Dashboard checks (push) Successful in 1m0s
Docker / Docker Build & Push (push) Successful in 4m11s

Accounts are centralized in AuthGuard now, so this project's own "first
launch, create the admin account" flow needs to know whether AuthGuard
already has one - not just whether *this* project's local setup has been
claimed. A fresh instance pointed at an AuthGuard that already has a
superadmin (created via AuthGuard's own web UI, or a sibling project's
setup) was incorrectly still showing its own create-account form.

Both the status check and the setup/registration handler now also consult
AuthGuard's GET /api/setup-status (via the vendored authclient's new
SetupStatus method) before deciding: if AuthGuard already has an account,
show the login screen instead, and reject a setup attempt that reaches the
handler anyway (e.g. a stale page) the same way an already-claimed local
setup is rejected.

Also updates the vendored shared/backend/authclient to add PublicUser's
totp_enabled field (AuthGuard has reported it for a while; the client
just hadn't picked it up).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Gabriel20xxandClaude Sonnet 5 committed 2026-09-02 10:32:55 +02:00
1 parent 4e39b5ac67
commit 51e0a4217b
3 files changed
+124 -17

No files matched your search

+49 -10
View File
@@ -77,19 +77,23 @@ import (
// ----------
// CollabTable's "first launch: create the first (superadmin) account" flow
// doesn't map onto AuthGuard, which is not scoped to a single project and
// has no concept of "this is CollabTable's initial account" -- and unlike a
// single-admin tool, "no accounts registered yet" can't be answered by
// asking AuthGuard either, since AuthGuard is a shared identity store other
// projects (and other CollabTable instances) may already have accounts in.
// Rather than trying to derive SetupRequired from AuthGuard state, CollabTable
// has no concept of "this is CollabTable's initial account". CollabTable
// keeps a tiny local flag of its own -- collabtable_setup_state.claimed
// (db.go) -- that means only "has this CollabTable instance completed its
// setup screen once", with no credentials of any kind in it. handleSetup
// atomically claims that flag, registers the account against AuthGuard's
// POST /api/register, and unclaims it again if registration fails (so a bad
// attempt doesn't permanently lock out setup). This is the smallest change
// that preserves the existing one-time-setup UX without forcing AuthGuard to
// answer a question it has no way to answer.
// attempt doesn't permanently lock out setup).
//
// The local claim alone would still show this "create account" form to a
// second, third, ... CollabTable instance's own first launch even though
// AuthGuard already has an account from the first one (or from AuthGuard's
// own web UI), since accounts are centralized -- there is no per-instance
// "initial account" once AuthGuard is shared. handleStatus and handleSetup
// both additionally call AuthGuard's GET /api/setup-status
// (authclient.Client.SetupStatus) and treat "AuthGuard already has an
// account" the same as "CollabTable's own setup is already claimed": show
// the login screen, refuse another registration through this flow.
//
// Known gaps versus the pre-migration behavior (AuthGuard was deliberately
// not modified for this migration, so these can only be closed by adding to
@@ -445,8 +449,25 @@ func (a *AuthService) handleStatus(w http.ResponseWriter, r *http.Request) {
return
}
if !claimed {
writeJSON(w, http.StatusOK, authStatusResponse{SetupRequired: true})
return
// The local claim alone isn't enough to decide this: accounts are
// centralized in AuthGuard now, so another CollabTable instance (or
// AuthGuard's own web UI) may have already created one even though
// this instance's own setup was never claimed. Showing the "create
// your account" form in that case would let someone spin up a
// redundant plain user instead of just logging into the account that
// already exists -- see setupRequired below.
setupRequired, err := a.authGuard.SetupStatus(r.Context())
if err != nil {
writeJSONError(w, http.StatusInternalServerError, errAuthGuardUnavailable.Error())
return
}
if setupRequired {
writeJSON(w, http.StatusOK, authStatusResponse{SetupRequired: true})
return
}
// AuthGuard already has an account; fall through to the ordinary
// "not authenticated" response so the frontend shows the login form
// instead of setup.
}
profile, _, err := a.currentProfile(r)
@@ -507,6 +528,24 @@ func (a *AuthService) handleSetup(w http.ResponseWriter, r *http.Request) {
return
}
// Belt-and-braces: handleStatus already keeps the frontend from ever
// showing this form once AuthGuard has any account (see its own
// SetupStatus check), but a stale page or a direct API call could still
// reach this handler. Reject it the same way as the local claim conflict
// above rather than creating a redundant plain user account.
if setupRequired, err := a.authGuard.SetupStatus(r.Context()); err != nil || !setupRequired {
if unclaimErr := a.db.UnclaimSetup(r.Context()); unclaimErr != nil {
writeJSONError(w, http.StatusInternalServerError, "Internal server error")
return
}
if err != nil {
writeJSONError(w, http.StatusInternalServerError, errAuthGuardUnavailable.Error())
return
}
writeJSONError(w, http.StatusConflict, errSetupComplete.Error())
return
}
result, err := a.authGuard.Register(r.Context(), username, email, request.Password)
if err != nil {
// The claim slot is only valid once actually backed by a real