From 117c46a3861f239c96ff1174e66190d53652e610 Mon Sep 17 00:00:00 2001 From: pptx704 Date: Mon, 13 Apr 2026 05:00:37 +0600 Subject: [PATCH] Fix: Auto-admin didn't work for oauth users --- internal/api/handlers_oauth.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/internal/api/handlers_oauth.go b/internal/api/handlers_oauth.go index 037929f..9209e86 100644 --- a/internal/api/handlers_oauth.go +++ b/internal/api/handlers_oauth.go @@ -195,6 +195,15 @@ func (h *oauthHandler) Callback(w http.ResponseWriter, r *http.Request) { qtx := h.db.WithTx(tx) + // The first user to sign up becomes a platform admin. + userCount, err := qtx.CountUsers(ctx) + if err != nil { + slog.Error("oauth: failed to count users", "error", err) + redirectWithError(w, r, redirectBase, "db_error") + return + } + isFirstUser := userCount == 0 + userID := id.NewUserID() _, err = qtx.InsertUserOAuth(ctx, db.InsertUserOAuthParams{ ID: userID, @@ -238,6 +247,14 @@ func (h *oauthHandler) Callback(w http.ResponseWriter, r *http.Request) { return } + if isFirstUser { + if err := qtx.SetUserAdmin(ctx, db.SetUserAdminParams{ID: userID, IsAdmin: true}); err != nil { + slog.Error("oauth: failed to set admin status", "error", err) + redirectWithError(w, r, redirectBase, "db_error") + return + } + } + if err := qtx.InsertOAuthProvider(ctx, db.InsertOAuthProviderParams{ Provider: provider, ProviderID: profile.ProviderID, @@ -255,7 +272,7 @@ func (h *oauthHandler) Callback(w http.ResponseWriter, r *http.Request) { return } - token, err := auth.SignJWT(h.jwtSecret, userID, teamID, email, profile.Name, "owner", false) + token, err := auth.SignJWT(h.jwtSecret, userID, teamID, email, profile.Name, "owner", isFirstUser) if err != nil { slog.Error("oauth: failed to sign jwt", "error", err) redirectWithError(w, r, redirectBase, "internal_error")