2025-08-07 13:47:42 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
|
|
|
|
|
|
"neomovies-api/pkg/middleware"
|
|
|
|
|
"neomovies-api/pkg/models"
|
|
|
|
|
"neomovies-api/pkg/services"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type AuthHandler struct {
|
|
|
|
|
authService *services.AuthService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewAuthHandler(authService *services.AuthService) *AuthHandler {
|
|
|
|
|
return &AuthHandler{
|
|
|
|
|
authService: authService,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var req models.RegisterRequest
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response, err := h.authService.Register(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusConflict)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
|
json.NewEncoder(w).Encode(models.APIResponse{
|
|
|
|
|
Success: true,
|
|
|
|
|
Data: response,
|
|
|
|
|
Message: "User registered successfully",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var req models.LoginRequest
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response, err := h.authService.Login(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// Определяем правильный статус код в зависимости от ошибки
|
|
|
|
|
statusCode := http.StatusBadRequest
|
|
|
|
|
if err.Error() == "Account not activated. Please verify your email." {
|
|
|
|
|
statusCode = http.StatusForbidden // 403 для неверифицированного email
|
|
|
|
|
}
|
|
|
|
|
http.Error(w, err.Error(), statusCode)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
json.NewEncoder(w).Encode(models.APIResponse{
|
|
|
|
|
Success: true,
|
|
|
|
|
Data: response,
|
|
|
|
|
Message: "Login successful",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *AuthHandler) GetProfile(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
userID, ok := middleware.GetUserIDFromContext(r.Context())
|
|
|
|
|
if !ok {
|
|
|
|
|
http.Error(w, "User ID not found in context", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user, err := h.authService.GetUserByID(userID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "User not found", http.StatusNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
json.NewEncoder(w).Encode(models.APIResponse{
|
|
|
|
|
Success: true,
|
|
|
|
|
Data: user,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *AuthHandler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
userID, ok := middleware.GetUserIDFromContext(r.Context())
|
|
|
|
|
if !ok {
|
|
|
|
|
http.Error(w, "User ID not found in context", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var updates map[string]interface{}
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&updates); err != nil {
|
|
|
|
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Удаляем поля, которые нельзя обновлять через этот эндпоинт
|
|
|
|
|
delete(updates, "password")
|
|
|
|
|
delete(updates, "email")
|
|
|
|
|
delete(updates, "_id")
|
|
|
|
|
delete(updates, "created_at")
|
|
|
|
|
|
|
|
|
|
user, err := h.authService.UpdateUser(userID, bson.M(updates))
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "Failed to update user", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
json.NewEncoder(w).Encode(models.APIResponse{
|
|
|
|
|
Success: true,
|
|
|
|
|
Data: user,
|
|
|
|
|
Message: "Profile updated successfully",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-08 10:35:07 +00:00
|
|
|
// Удаление аккаунта
|
|
|
|
|
func (h *AuthHandler) DeleteAccount(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
userID, ok := middleware.GetUserIDFromContext(r.Context())
|
|
|
|
|
if !ok {
|
|
|
|
|
http.Error(w, "User ID not found in context", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := h.authService.DeleteAccount(r.Context(), userID); err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
json.NewEncoder(w).Encode(models.APIResponse{
|
|
|
|
|
Success: true,
|
|
|
|
|
Message: "Account deleted successfully",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
// Подтверждение email
|
2025-08-07 13:47:42 +00:00
|
|
|
func (h *AuthHandler) VerifyEmail(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var req models.VerifyEmailRequest
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response, err := h.authService.VerifyEmail(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
json.NewEncoder(w).Encode(response)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Повторная отправка кода верификации
|
|
|
|
|
func (h *AuthHandler) ResendVerificationCode(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var req models.ResendCodeRequest
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response, err := h.authService.ResendVerificationCode(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
json.NewEncoder(w).Encode(response)
|
|
|
|
|
}
|