modified: serv_nginx/api_bb/internal/handlers/auth.go

add logger into updateProfile function
This commit is contained in:
2025-10-11 06:25:50 +05:00
parent c79a642503
commit 664232db0f
+21 -9
View File
@@ -37,7 +37,7 @@ func (h *AuthHandler) Routes() chi.Router {
r.Options("/login", h.handleOptions)
r.Options("/logout", h.handleOptions)
r.Options("/profile", h.handleOptions)
r.Options("/editProfile", h.handleOptions)
r.Options("/editProfile", h.handleOptions)
r.Post("/register", h.Register)
r.Post("/login", h.Login)
@@ -97,10 +97,10 @@ func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
utils.RespondWithError(w, http.StatusBadRequest, "Failed to read request body: "+err.Error())
return
}
// Восстанавливаем тело для дальнейшего использования
r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
fmt.Printf("Raw request body: %s\n", string(bodyBytes))
var req RegisterRequest
@@ -133,7 +133,7 @@ func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
utils.RespondWithError(w, http.StatusBadRequest, "Password must be at least 6 characters")
return
}
user := &models.User{
Email: req.Email,
Password: req.Password,
@@ -147,7 +147,7 @@ func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
if err := h.authService.Register(user); err != nil {
fmt.Printf("Auth service error: %v\n", err)
utils.RespondWithError(w, http.StatusBadRequest, err.Error())
@@ -265,10 +265,22 @@ type UpdateProfileRequest struct {
func (h *AuthHandler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
// Устанавливаем CORS заголовки
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Methods", "PUT, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Methods", "PUT, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
// Логируем тело запроса для отладки
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
utils.RespondWithError(w, http.StatusBadRequest, "Failed to read request body: "+err.Error())
return
}
// Восстанавливаем тело для дальнейшего использования
r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
fmt.Printf("Raw request body: %s\n", string(bodyBytes))
// Получаем пользователя из контекста
currentUser, ok := middleware.GetUserFromContext(r.Context())