add get user data method
This commit is contained in:
35
api/auth/get_user_data.go
Normal file
35
api/auth/get_user_data.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user