package auth import ( "errors" "net/http" "github.com/gin-gonic/gin" ) type Handler struct { service *Service } func NewHandler(service *Service) *Handler { return &Handler{service: service} } // @Summary Epta registration // @Description Create user account with username, email, password // @Tags auth // @Accept json // @Produce json // @Param request body RegisterRequest true "Registration details" // @Success 201 {object} UserResponse // @Failure 400 {object} ErrorResponse // @Failure 409 {object} ErrorResponse // @Router /api/auth/register [post] func (h *Handler) Register(c *gin.Context) { var req RegisterRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, ErrorResponse{Error: err.Error()}) return } user, err := h.service.Register(c.Request.Context(), req) if err != nil { if errors.Is(err, ErrEmailExists) { c.JSON(http.StatusConflict, ErrorResponse{Error: err.Error()}) return } c.JSON(http.StatusInternalServerError, ErrorResponse{Error: "internal server error"}) return } c.JSON(http.StatusCreated, UserResponse{User: *user}) } // @Summary Epta login // @Description Authenticate user with email and password, returns JWT token // @Tags auth // @Accept json // @Produce json // @Param request body LoginRequest true "Login credentials" // @Success 200 {object} AuthResponse // @Failure 400 {object} ErrorResponse // @Failure 401 {object} ErrorResponse // @Router /api/auth/login [post] func (h *Handler) Login(c *gin.Context) { var req LoginRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, ErrorResponse{Error: err.Error()}) return } resp, err := h.service.Login(c.Request.Context(), req) if err != nil { c.JSON(http.StatusUnauthorized, ErrorResponse{Error: err.Error()}) return } c.JSON(http.StatusOK, resp) } // @Summary Epta get current user // @Description Get authenticated user's profile // @Tags auth // @Accept json // @Produce json // @Security Bearer // @Success 200 {object} UserResponse // @Failure 401 {object} ErrorResponse // @Router /api/auth/me [get] func (h *Handler) Me(c *gin.Context) { rawUserID, exists := c.Get("user_id") if !exists { c.JSON(http.StatusUnauthorized, ErrorResponse{Error: "unauthorized"}) return } userID, ok := rawUserID.(string) if !ok { c.JSON(http.StatusInternalServerError, ErrorResponse{Error: "invalid user ID in context"}) return } user, err := h.service.GetUserByID(c.Request.Context(), userID) if err != nil { if errors.Is(err, ErrUserNotFound) || errors.Is(err, ErrInvalidUserID) { c.JSON(http.StatusNotFound, ErrorResponse{Error: err.Error()}) return } c.JSON(http.StatusInternalServerError, ErrorResponse{Error: "internal server error"}) return } c.JSON(http.StatusOK, UserResponse{User: *user}) }