wishli-api/api/handlers/auth_handler.go
2025-03-23 20:05:51 +03:00

162 lines
5.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package handlers
import (
"net/http"
"wish-list-api/api/presenter"
"wish-list-api/pkg/auth"
"wish-list-api/pkg/entities"
"github.com/gofiber/fiber/v2"
)
// @Summary Вход пользователя
// @Description Аутентифицирует пользователя и выдает JWT токены
// @Tags auth
// @Accept json
// @Produce json
// @Param credentials body entities.LoginRequest true "Учетные данные пользователя"
// @Success 200 {object} presenter.AuthResponse
// @Failure 400 {object} presenter.AuthResponse
// @Failure 401 {object} presenter.AuthResponse
// @Failure 500 {object} presenter.AuthResponse
// @Router /auth/login [post]
func Login(service auth.Service) fiber.Handler {
return func(c *fiber.Ctx) error {
var requestBody entities.LoginRequest
err := c.BodyParser(&requestBody)
if err != nil {
c.Status(http.StatusBadRequest)
return c.JSON(presenter.AuthErrorResponse(err))
}
if requestBody.Email == "" || requestBody.Password == "" {
c.Status(http.StatusBadRequest)
return c.JSON(presenter.AuthErrorResponse(fiber.ErrBadRequest))
}
tokens, err := service.Login(&requestBody)
if err != nil {
c.Status(http.StatusUnauthorized)
return c.JSON(presenter.AuthErrorResponse(err))
}
return c.JSON(presenter.AuthSuccessResponse(tokens))
}
}
// @Summary Регистрация пользователя
// @Description Регистрирует нового пользователя и выдает JWT токены
// @Tags auth
// @Accept json
// @Produce json
// @Param user body entities.RegisterRequest true "Данные нового пользователя"
// @Success 200 {object} presenter.AuthResponse
// @Failure 400 {object} presenter.AuthResponse
// @Failure 409 {object} presenter.AuthResponse
// @Failure 500 {object} presenter.AuthResponse
// @Router /auth/register [post]
func Register(service auth.Service) fiber.Handler {
return func(c *fiber.Ctx) error {
var requestBody entities.RegisterRequest
err := c.BodyParser(&requestBody)
if err != nil {
c.Status(http.StatusBadRequest)
return c.JSON(presenter.AuthErrorResponse(err))
}
if requestBody.Email == "" || requestBody.Password == "" {
c.Status(http.StatusBadRequest)
return c.JSON(presenter.AuthErrorResponse(fiber.ErrBadRequest))
}
user, err := service.Register(&requestBody)
if err != nil {
c.Status(http.StatusConflict)
return c.JSON(presenter.AuthErrorResponse(err))
}
tokens, err := service.Login(&entities.LoginRequest{
Email: requestBody.Email,
Password: requestBody.Password,
})
if err != nil {
c.Status(http.StatusInternalServerError)
return c.JSON(presenter.AuthErrorResponse(err))
}
return c.JSON(presenter.AuthSuccessResponseWithUser(tokens, user))
}
}
// @Summary Обновление токенов
// @Description Обновляет JWT токены с помощью refresh токена
// @Tags auth
// @Accept json
// @Produce json
// @Param refreshToken body entities.TokenRequest true "Refresh токен"
// @Success 200 {object} presenter.AuthResponse
// @Failure 400 {object} presenter.AuthResponse
// @Failure 401 {object} presenter.AuthResponse
// @Failure 500 {object} presenter.AuthResponse
// @Router /auth/refresh [post]
func RefreshToken(service auth.Service) fiber.Handler {
return func(c *fiber.Ctx) error {
var requestBody entities.TokenRequest
err := c.BodyParser(&requestBody)
if err != nil {
c.Status(http.StatusBadRequest)
return c.JSON(presenter.AuthErrorResponse(err))
}
if requestBody.RefreshToken == "" {
c.Status(http.StatusBadRequest)
return c.JSON(presenter.AuthErrorResponse(fiber.ErrBadRequest))
}
tokens, err := service.RefreshToken(requestBody.RefreshToken)
if err != nil {
c.Status(http.StatusUnauthorized)
return c.JSON(presenter.AuthErrorResponse(err))
}
return c.JSON(presenter.AuthSuccessResponse(tokens))
}
}
// @Summary Вход пользователя через Telegram
// @Description Аутентифицирует пользователя через Telegram и выдает JWT токены
// @Tags auth
// @Accept json
// @Produce json
// @Param credentials body entities.TelegramAuthRequest true "Данные аутентификации Telegram"
// @Success 200 {object} presenter.AuthResponse
// @Failure 400 {object} presenter.AuthResponse
// @Failure 401 {object} presenter.AuthResponse
// @Failure 500 {object} presenter.AuthResponse
// @Router /auth/telegram [post]
func LoginWithTelegram(telegramService auth.TelegramAuthService) fiber.Handler {
return func(c *fiber.Ctx) error {
var requestBody entities.TelegramAuthRequest
err := c.BodyParser(&requestBody)
if err != nil {
c.Status(http.StatusBadRequest)
return c.JSON(presenter.AuthErrorResponse(err))
}
if requestBody.TelegramID == 0 || requestBody.AuthDate == 0 || requestBody.Hash == "" {
c.Status(http.StatusBadRequest)
return c.JSON(presenter.AuthErrorResponse(fiber.ErrBadRequest))
}
tokens, err := telegramService.AuthenticateWithTelegram(&requestBody)
if err != nil {
c.Status(http.StatusUnauthorized)
return c.JSON(presenter.AuthErrorResponse(err))
}
c.Status(http.StatusOK)
return c.JSON(presenter.AuthSuccessResponse(tokens))
}
}