36 lines
808 B
Go
36 lines
808 B
Go
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
|
|
}
|
|
}
|