Добавлен основные классы для сервиса авторизаци

This commit is contained in:
Ганеев Артем
2025-10-28 20:49:54 +03:00
parent def3552a67
commit 736b8031f8
26 changed files with 904 additions and 40 deletions

View File

@@ -1,13 +1,65 @@
package handler
import (
"authorization/internal"
"net/http"
"github.com/gin-gonic/gin"
)
func (h *Handler) signUp(c *gin.Context) {
var input internal.User
if err := c.BindJSON(&input); err != nil {
newErrorResponse(c, http.StatusBadRequest, err.Error())
return
}
id, err := h.services.Authorization.CreateUser(input)
if err != nil {
newErrorResponse(c, http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, map[string]interface{}{
"id": id,
})
}
func (h *Handler) signIn(c *gin.Context) {
var input internal.AuthUser
if err := c.BindJSON(&input); err != nil {
newErrorResponse(c, http.StatusBadRequest, err.Error())
return
}
accesstoken, refreshToken, err := h.services.Authorization.GenerateToken(input.Username, input.Password)
if err != nil {
newErrorResponse(c, http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, map[string]interface{}{
"accessToken": accesstoken,
"refreshToken": refreshToken,
})
}
func (h *Handler) refresh(c *gin.Context) {
var input internal.RefreshTokenRequest
if err := c.BindJSON(&input); err != nil {
newErrorResponse(c, http.StatusBadRequest, err.Error())
return
}
accessToken, refreshToken, err := h.services.Authorization.RefreshToken(input.RefreshToken)
if err != nil {
newErrorResponse(c, http.StatusUnauthorized, err.Error())
return
}
c.JSON(http.StatusOK, map[string]interface{}{
"accessToken": accessToken,
"refreshToken": refreshToken,
})
}