You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.3 KiB
53 lines
1.3 KiB
package controllers |
|
|
|
import ( |
|
"net/http" |
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
"autostore/internal/application/commands" |
|
"autostore/internal/application/dto" |
|
"autostore/internal/application/interfaces" |
|
) |
|
|
|
type AuthController struct { |
|
loginUserCmd *commands.LoginUserCommand |
|
logger interfaces.ILogger |
|
} |
|
|
|
func NewAuthController( |
|
loginUserCmd *commands.LoginUserCommand, |
|
logger interfaces.ILogger, |
|
) *AuthController { |
|
return &AuthController{ |
|
loginUserCmd: loginUserCmd, |
|
logger: logger, |
|
} |
|
} |
|
|
|
func (ctrl *AuthController) Login(c *gin.Context) { |
|
var loginDTO dto.LoginDTO |
|
if err := c.ShouldBindJSON(&loginDTO); err != nil { |
|
c.JSON(http.StatusBadRequest, dto.JSendError("Invalid request body", http.StatusBadRequest)) |
|
return |
|
} |
|
|
|
if err := loginDTO.Validate(); err != nil { |
|
c.JSON(http.StatusBadRequest, dto.JSendError(err.Error(), http.StatusBadRequest)) |
|
return |
|
} |
|
|
|
token, err := ctrl.loginUserCmd.Execute(c.Request.Context(), loginDTO.Username, loginDTO.Password) |
|
if err != nil { |
|
c.JSON(http.StatusUnauthorized, dto.JSendError(err.Error(), http.StatusUnauthorized)) |
|
return |
|
} |
|
|
|
response := &dto.LoginResponseDTO{ |
|
Token: token, |
|
TokenType: "Bearer", |
|
ExpiresIn: 3600, // 1 hour in seconds |
|
} |
|
|
|
c.JSON(http.StatusOK, dto.JSendSuccess(response)) |
|
} |