Skip to content

Commit c772f28

Browse files
committed
fix: add GetMe in user service instead of using a DB_URL conditions...
1 parent be7592e commit c772f28

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

internal/handlers/dashboard/users_handler.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package handlers
33
import (
44
"encoding/json"
55
"errors"
6-
"expo-open-ota/config"
76
"expo-open-ota/internal/handlers"
87
"expo-open-ota/internal/middleware"
98
"expo-open-ota/internal/services"
@@ -95,19 +94,15 @@ func renderUserServiceError(w http.ResponseWriter, err error) {
9594
}
9695

9796
// GetMeHandler answers with the account behind the current session. In
98-
// control-plane mode the row is re-read so the dashboard sees a revoked admin
99-
// flag on its next load, not at token refresh.
97+
// control-plane mode the service re-reads the row so the dashboard sees a
98+
// revoked admin flag on its next load, not at token refresh.
10099
func (h *UsersHandler) GetMeHandler(w http.ResponseWriter, r *http.Request) {
101100
principal := middleware.PrincipalFromContext(r.Context())
102101
if principal == nil {
103102
handlers.RenderError(w, http.StatusUnauthorized, "This route requires a dashboard session")
104103
return
105104
}
106-
if !config.IsDBMode() {
107-
renderJSON(w, http.StatusOK, UserResponse{Email: principal.Email, IsAdmin: true, Enabled: true})
108-
return
109-
}
110-
user, err := h.userService.GetUserByID(r.Context(), principal.UserId)
105+
user, err := h.userService.GetMe(r.Context(), principal.UserId, principal.Email)
111106
if err != nil {
112107
// Only a missing row means the session is a leftover of a deleted
113108
// account; an infrastructure failure must not read as a dead session.

internal/services/user_service.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ func (s *UserService) GetUserByID(ctx context.Context, id string) (store.User, e
9696
return s.userRepo.GetUserByID(ctx, id)
9797
}
9898

99+
func (s *UserService) GetMe(ctx context.Context, userId string, email string) (store.User, error) {
100+
if s.userRepo == nil {
101+
return store.User{Email: email, IsAdmin: true, Enabled: true}, nil
102+
}
103+
return s.userRepo.GetUserByID(ctx, userId)
104+
}
105+
99106
func (s *UserService) CreateUser(ctx context.Context, email string, password string, isAdmin bool) (store.User, error) {
100107
if err := s.requireControlPlane(); err != nil {
101108
return store.User{}, err

0 commit comments

Comments
 (0)