/
mpstrkv
/
SpbTechRun
Обзор
Документация
Войти
/
mpstrkv
/
SpbTechRun
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
backend/internal/handler/auth.go
97 строк
3 KB
mihailpestrikov
Initial commit: SpbTechRun recommendation system
05 дек 2025, 16:11
05 дек 2025, 16:11
8ba7137
Код
Авторство
О чём код?
package handler import ( "errors" "net/http" "github.com/gin-gonic/gin" "github.com/mpstrkv/spbtechrun/internal/dto" "github.com/mpstrkv/spbtechrun/internal/middleware" "github.com/mpstrkv/spbtechrun/internal/service" ) const sessionCookieNameAuth = "session_id" type AuthHandler struct { authService *service.AuthService cartService *service.CartService } func NewAuthHandler(authService *service.AuthService, cartService *service.CartService) *AuthHandler { return &AuthHandler{ authService: authService, cartService: cartService, } } func (h *AuthHandler) Register(c *gin.Context) { var req dto.RegisterRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } user, token, err := h.authService.Register(c.Request.Context(), req.Email, req.Password, req.Name, req.Phone) if err != nil { if errors.Is(err, service.ErrUserExists) { c.JSON(http.StatusConflict, gin.H{"error": "user with this email already exists"}) return } c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusCreated, dto.AuthResponse{ Token: token, User: dto.UserToResponse(user), }) } func (h *AuthHandler) Login(c *gin.Context) { var req dto.LoginRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } user, token, err := h.authService.Login(c.Request.Context(), req.Email, req.Password) if err != nil { if errors.Is(err, service.ErrUserNotFound) { c.JSON(http.StatusUnauthorized, gin.H{"error": "account not found"}) return } if errors.Is(err, service.ErrInvalidPassword) { c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid password"}) return } c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } if sessionID, err := c.Cookie(sessionCookieNameAuth); err == nil && sessionID != "" { _ = h.cartService.MergeGuestCart(c.Request.Context(), sessionID, user.ID) c.SetCookie(sessionCookieNameAuth, "", -1, "/", "", false, true) } c.JSON(http.StatusOK, dto.AuthResponse{ Token: token, User: dto.UserToResponse(user), }) } func (h *AuthHandler) Profile(c *gin.Context) { userID, ok := middleware.GetUserID(c) if !ok { c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) return } user, err := h.authService.GetUserByID(c.Request.Context(), userID) if err != nil { c.JSON(http.StatusNotFound, gin.H{"error": "user not found"}) return } c.JSON(http.StatusOK, dto.UserToResponse(user)) }