66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
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,
|
|
})
|
|
}
|