From 7b8fe6baf2a5e679f9d05a780ad2ff4541cd66f0 Mon Sep 17 00:00:00 2001 From: LandaMm Date: Wed, 4 Jun 2025 20:07:39 +0200 Subject: [PATCH] fix: set content-type to json --- internal/admin/apiservices.go | 11 +++++++++++ internal/admin/users.go | 4 ++++ internal/auth/routes.go | 7 +++++++ internal/oauth/openid.go | 3 +++ internal/user/routes.go | 4 ++++ 5 files changed, 29 insertions(+) diff --git a/internal/admin/apiservices.go b/internal/admin/apiservices.go index 882d611..526d95b 100644 --- a/internal/admin/apiservices.go +++ b/internal/admin/apiservices.go @@ -62,6 +62,8 @@ func (h *AdminHandler) GetApiServices(w http.ResponseWriter, r *http.Request) { encoder := json.NewEncoder(w) + w.Header().Set("Content-Type", "application/json") + if err := encoder.Encode(Response{ Items: apiServices, Count: len(apiServices), @@ -147,6 +149,9 @@ func (h *AdminHandler) AddApiService(w http.ResponseWriter, r *http.Request) { } encoder := json.NewEncoder(w) + + w.Header().Set("Content-Type", "application/json") + if err := encoder.Encode(Response{ Service: NewApiServiceDTO(service), Credentials: ApiServiceCredentials{ @@ -174,6 +179,8 @@ func (h *AdminHandler) GetApiService(w http.ResponseWriter, r *http.Request) { encoder := json.NewEncoder(w) + w.Header().Set("Content-Type", "application/json") + if err := encoder.Encode(NewApiServiceDTO(service)); err != nil { web.Error(w, "failed to encode response", http.StatusInternalServerError) } @@ -209,6 +216,8 @@ func (h *AdminHandler) RegenerateApiServiceSecret(w http.ResponseWriter, r *http encoder := json.NewEncoder(w) + w.Header().Set("Content-Type", "application/json") + if err := encoder.Encode(ApiServiceCredentials{ ClientId: service.ClientID, ClientSecret: clientSecret, @@ -272,6 +281,8 @@ func (h *AdminHandler) UpdateApiService(w http.ResponseWriter, r *http.Request) encoder := json.NewEncoder(w) + w.Header().Set("Content-Type", "application/json") + if err := encoder.Encode(NewApiServiceDTO(updated)); err != nil { web.Error(w, "failed to send updated api service", http.StatusInternalServerError) } diff --git a/internal/admin/users.go b/internal/admin/users.go index 9372da4..4bfe297 100644 --- a/internal/admin/users.go +++ b/internal/admin/users.go @@ -47,6 +47,8 @@ func (h *AdminHandler) GetUsers(w http.ResponseWriter, r *http.Request) { encoder := json.NewEncoder(w) + w.Header().Set("Content-Type", "application/json") + if err := encoder.Encode(&Response{ Items: items, Count: len(items), @@ -71,6 +73,8 @@ func (h *AdminHandler) GetUser(w http.ResponseWriter, r *http.Request) { encoder := json.NewEncoder(w) + w.Header().Set("Content-Type", "application/json") + if err := encoder.Encode(NewUserDTO(&user)); err != nil { web.Error(w, "failed to encode user dto", http.StatusInternalServerError) } diff --git a/internal/auth/routes.go b/internal/auth/routes.go index 0dcdb73..c450453 100644 --- a/internal/auth/routes.go +++ b/internal/auth/routes.go @@ -137,6 +137,8 @@ func (h *AuthHandler) refreshToken(w http.ResponseWriter, r *http.Request) { encoder := json.NewEncoder(w) + w.Header().Set("Content-Type", "application/json") + if err := encoder.Encode(Response{ AccessToken: access, RefreshToken: refresh, @@ -213,6 +215,11 @@ func (h *AuthHandler) login(w http.ResponseWriter, r *http.Request) { return } + if err := h.repo.UpdateLastLogin(r.Context(), user.ID); err != nil { + web.Error(w, "failed to update user's last login", http.StatusInternalServerError) + return + } + encoder := json.NewEncoder(w) type Response struct { diff --git a/internal/oauth/openid.go b/internal/oauth/openid.go index ede2f63..6d8de29 100644 --- a/internal/oauth/openid.go +++ b/internal/oauth/openid.go @@ -19,6 +19,9 @@ func (h *OAuthHandler) OpenIdConfiguration(w http.ResponseWriter, r *http.Reques w.Header().Set("Content-Type", "application/json") encoder := json.NewEncoder(w) + + w.Header().Set("Content-Type", "application/json") + if err := encoder.Encode(Response{ TokenEndpoint: h.cfg.Jwt.Issuer + "/api/v1/oauth/token", AuthorizationEndpoint: h.cfg.Jwt.Issuer + "/api/v1/oauth/authorize", diff --git a/internal/user/routes.go b/internal/user/routes.go index 864b702..42cabcf 100644 --- a/internal/user/routes.go +++ b/internal/user/routes.go @@ -98,6 +98,8 @@ func (h *UserHandler) register(w http.ResponseWriter, r *http.Request) { Id string `json:"id"` } + w.Header().Set("Content-Type", "application/json") + if err := encoder.Encode(Response{ Id: id.String(), }); err != nil { @@ -184,6 +186,8 @@ func (h *UserHandler) uploadAvatar(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) encoder := json.NewEncoder(w) + w.Header().Set("Content-Type", "application/json") + if err := encoder.Encode(Response{AvatarID: uploadInfo.Key}); err != nil { web.Error(w, "failed to write response", http.StatusInternalServerError) }