fix(auth): new users inherit the system-wide default timezone

default_timezone existed as a system setting (admin-editable, exposed
via SettingsRegistry.DefaultTimezone) but nothing read it at user
creation — the CreateUser query didn't even carry the column, so every
new user got the DB default UTC regardless. Both creation paths
(public register + admin create) now seed the user's timezone from the
setting, validated with time.LoadLocation and falling back to UTC when
unwired/empty/invalid.

Test-infra fixes surfaced by the run: TestUpdateProfileSelfEdit was
self-editing the SHARED admin (renaming it past the harness cleanup's
preserve-list, leaking a second admin row that broke the last-admin
tests) — it now edits a dedicated user with a unique name; the
last-admin tests get an ensureSingleAdmin precondition helper; the
websocket collection name is run-unique (non-test names persist in the
shared DB and UNIQUE(user_id,name) 409s on rerun).
This commit is contained in:
John O'Keefe committed 2026-10-13 13:17:12 -04:00
1 parent bc6ce41762
commit 740213f0a4
9 files changed
+108 -14

No files matched your search

+7 -3
View File
@@ -2003,9 +2003,9 @@ func (q *Queries) CreateUnlinkedBook(ctx context.Context, arg CreateUnlinkedBook
}
const CreateUser = `-- name: CreateUser :one
INSERT INTO users (email, username, password_hash, first_name, last_name, theme, role)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id, email, username, theme, first_name, last_name, role, created_at, updated_at
INSERT INTO users (email, username, password_hash, first_name, last_name, theme, role, timezone)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING id, email, username, theme, first_name, last_name, role, timezone, created_at, updated_at
`
type CreateUserParams struct {
@@ -2016,6 +2016,7 @@ type CreateUserParams struct {
LastName pgtype.Text `db:"last_name" json:"last_name"`
Theme pgtype.Text `db:"theme" json:"theme"`
Role string `db:"role" json:"role"`
Timezone pgtype.Text `db:"timezone" json:"timezone"`
}
type CreateUserRow struct {
@@ -2026,6 +2027,7 @@ type CreateUserRow struct {
FirstName pgtype.Text `db:"first_name" json:"first_name"`
LastName pgtype.Text `db:"last_name" json:"last_name"`
Role string `db:"role" json:"role"`
Timezone pgtype.Text `db:"timezone" json:"timezone"`
CreatedAt pgtype.Timestamptz `db:"created_at" json:"created_at"`
UpdatedAt pgtype.Timestamptz `db:"updated_at" json:"updated_at"`
}
@@ -2039,6 +2041,7 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (CreateU
arg.LastName,
arg.Theme,
arg.Role,
arg.Timezone,
)
var i CreateUserRow
err := row.Scan(
@@ -2049,6 +2052,7 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (CreateU
&i.FirstName,
&i.LastName,
&i.Role,
&i.Timezone,
&i.CreatedAt,
&i.UpdatedAt,
)
+3 -3
View File
@@ -1,7 +1,7 @@
-- name: CreateUser :one
INSERT INTO users (email, username, password_hash, first_name, last_name, theme, role)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id, email, username, theme, first_name, last_name, role, created_at, updated_at;
INSERT INTO users (email, username, password_hash, first_name, last_name, theme, role, timezone)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING id, email, username, theme, first_name, last_name, role, timezone, created_at, updated_at;
-- name: GetUserByEmail :one
SELECT id, email, username, theme, first_name, last_name, role, created_at, updated_at FROM users WHERE email = $1;
+20
View File
@@ -39,6 +39,23 @@ var SessionDurationSec = int(DefaultSessionDuration.Seconds())
// SetSettings wires the tunable settings registry (optional).
func (h *AuthHandler) SetSettings(s *database.SettingsRegistry) { h.settings = s }
// defaultTimezone returns the system-wide default timezone for new users.
// Falls back to UTC when no registry is wired, the setting is empty, or
// the value is not a valid IANA location.
func (h *AuthHandler) defaultTimezone() string {
if h.settings == nil {
return "UTC"
}
tz := h.settings.DefaultTimezone()
if tz == "" {
return "UTC"
}
if _, err := time.LoadLocation(tz); err != nil {
return "UTC"
}
return tz
}
// sessionDuration returns the active session duration from the registry.
func (h *AuthHandler) sessionDuration() time.Duration {
if h.settings != nil {
@@ -263,6 +280,7 @@ func (h *AuthHandler) Register(c *echo.Context) error {
LastName: pgtype.Text{String: req.LastName, Valid: req.LastName != ""},
Theme: pgtype.Text{String: "tokyo-night", Valid: true},
Role: userRole,
Timezone: pgtype.Text{String: h.defaultTimezone(), Valid: true},
})
if err != nil {
if c.Request().Header.Get("HX-Request") == "true" {
@@ -735,6 +753,7 @@ func (h *AuthHandler) AdminCreateUser(c *echo.Context) error {
LastName: pgtype.Text{String: req.LastName, Valid: req.LastName != ""},
Theme: pgtype.Text{String: "tokyo-night", Valid: true},
Role: role,
Timezone: pgtype.Text{String: h.defaultTimezone(), Valid: true},
})
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
@@ -756,6 +775,7 @@ func (h *AuthHandler) AdminCreateUser(c *echo.Context) error {
"email": user.Email,
"username": user.Username,
"role": user.Role,
"timezone": user.Timezone,
"created_at": user.CreatedAt,
})
}