refactor: migrate from raw pgx to GORM, unify ErrNoRows, cleanup auth
This commit is contained in:
+22
-38
@@ -5,6 +5,7 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"gitea.d3m0k1d.ru/HellreigN/Control-plane/internal/api"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -16,8 +17,8 @@ func NewHandler(service *Service) *Handler {
|
||||
return &Handler{service: service}
|
||||
}
|
||||
|
||||
// @Summary Register epta
|
||||
// @Description Create user account with username, email, password
|
||||
// @Summary Register
|
||||
// @Description Создание учетной записи пользователя с полями username, email, password
|
||||
// @Tags auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -52,7 +53,7 @@ func (h *Handler) Register(c *gin.Context) {
|
||||
}
|
||||
|
||||
// @Summary Login
|
||||
// @Description Authenticate user with email and password, returns JWT token
|
||||
// @Description Аунтефикация пользователя с помощью email и password, возвращает JWT token
|
||||
// @Tags auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -82,8 +83,8 @@ func (h *Handler) Login(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
// @Summary Refresh epta token
|
||||
// @Description Get a new access token using a refresh token
|
||||
// @Summary Refresh token
|
||||
// @Description Получение ново
|
||||
// @Tags auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -113,8 +114,8 @@ func (h *Handler) Refresh(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
// @Summary Logout epta
|
||||
// @Description Invalidate a refresh token (logout)
|
||||
// @Summary Logout
|
||||
// @Description Аннулирует refresh token
|
||||
// @Tags auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -143,8 +144,8 @@ func (h *Handler) Logout(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "logged out successfully"})
|
||||
}
|
||||
|
||||
// @Summary Get epta current user
|
||||
// @Description Get authenticated user's profile
|
||||
// @Summary Get current user
|
||||
// @Description Получить профиль авторизованного пользователя
|
||||
// @Tags auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -153,18 +154,12 @@ func (h *Handler) Logout(c *gin.Context) {
|
||||
// @Failure 401 {object} ErrorResponse
|
||||
// @Router /api/auth/me [get]
|
||||
func (h *Handler) Me(c *gin.Context) {
|
||||
rawUserID, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
userID := api.GetUserID(c)
|
||||
if userID == "" {
|
||||
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) {
|
||||
@@ -179,8 +174,8 @@ func (h *Handler) Me(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, UserResponse{User: *user})
|
||||
}
|
||||
|
||||
// @Summary Change epta password
|
||||
// @Description Change current user's password
|
||||
// @Summary Change password
|
||||
// @Description Изменить текущий password пользователя
|
||||
// @Tags auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -191,18 +186,12 @@ func (h *Handler) Me(c *gin.Context) {
|
||||
// @Failure 401 {object} ErrorResponse
|
||||
// @Router /api/auth/password [put]
|
||||
func (h *Handler) ChangePassword(c *gin.Context) {
|
||||
rawUserID, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
userID := api.GetUserID(c)
|
||||
if userID == "" {
|
||||
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
|
||||
}
|
||||
|
||||
var req PasswordChangeRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, ErrorResponse{Error: err.Error()})
|
||||
@@ -210,7 +199,8 @@ func (h *Handler) ChangePassword(c *gin.Context) {
|
||||
}
|
||||
|
||||
if err := h.service.ChangePassword(c.Request.Context(), userID, req); err != nil {
|
||||
if errors.Is(err, ErrWrongPassword) || errors.Is(err, ErrSamePassword) || errors.Is(err, ErrWeakPassword) {
|
||||
if errors.Is(err, ErrWrongPassword) || errors.Is(err, ErrSamePassword) ||
|
||||
errors.Is(err, ErrWeakPassword) {
|
||||
c.JSON(http.StatusBadRequest, ErrorResponse{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
@@ -226,8 +216,8 @@ func (h *Handler) ChangePassword(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "password changed successfully"})
|
||||
}
|
||||
|
||||
// @Summary Update epta profile
|
||||
// @Description Update current user's username
|
||||
// @Summary Update profile
|
||||
// @Description Обновить username текущего пользователя
|
||||
// @Tags auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -238,18 +228,12 @@ func (h *Handler) ChangePassword(c *gin.Context) {
|
||||
// @Failure 401 {object} ErrorResponse
|
||||
// @Router /api/auth/me [put]
|
||||
func (h *Handler) UpdateProfile(c *gin.Context) {
|
||||
rawUserID, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
userID := api.GetUserID(c)
|
||||
if userID == "" {
|
||||
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
|
||||
}
|
||||
|
||||
var req UpdateProfileRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, ErrorResponse{Error: err.Error()})
|
||||
|
||||
Reference in New Issue
Block a user