diff --git a/api/auth/get_user_data.go b/api/auth/get_user_data.go new file mode 100644 index 0000000..a74a65d --- /dev/null +++ b/api/auth/get_user_data.go @@ -0,0 +1,35 @@ +package api_auth + +import ( + "encoding/json" + "net/http" +) + +type GetUserDataResponse struct { + UserID uint `json:"id"` + Username string `json:"username"` + Email string `json:"email"` +} + +func (h *authHandler) handleGetUserData(w http.ResponseWriter, r *http.Request) { + by := r.URL.Query().Get("by") + value := r.URL.Query().Get("value") + if value == "" { + value = r.URL.Query().Get(by) + } + user, err := h.a.Get(by, value) + if err != nil { + http.Error(w, "Failed to get user", http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + err = json.NewEncoder(w).Encode(meResponse{ + UserID: user.ID, + Username: user.Username, + Email: user.Email, + }) + if err != nil { + http.Error(w, "Failed to encode response", http.StatusInternalServerError) + return + } +}