Files
status/router/controller/auth/plain.go
2025-11-04 19:52:26 +03:00

71 lines
1.5 KiB
Go

package auth
import (
"net/http"
"git.ostiwe.com/ostiwe-com/status/dto"
"git.ostiwe.com/ostiwe-com/status/modules/auth"
httpApp "git.ostiwe.com/ostiwe-com/status/pkg/http"
"git.ostiwe.com/ostiwe-com/status/router/controller"
"github.com/gin-gonic/gin"
)
type PlainAuthController struct {
authModule auth.Module
}
func (c *PlainAuthController) Handler() gin.HandlerFunc {
return func(ginCtx *gin.Context) {
var payload dto.LoginRequest
if err := ginCtx.ShouldBindJSON(&payload); err != nil {
sendErr := httpApp.
NewResponseErrBuilder().
WithStatusCode(http.StatusBadRequest).
WithMessage(err.Error()).
Send(ginCtx)
if sendErr != nil {
ginCtx.Writer.WriteHeader(http.StatusInternalServerError)
return
}
return
}
jwtString, err := c.authModule.Proceed(ginCtx.Request.Context(), payload.Login, payload.Password)
if err != nil {
sendErr := httpApp.
NewResponseErrBuilder().
WithStatusCode(http.StatusBadRequest).
WithMessage(err.Error()).
Send(ginCtx)
if sendErr != nil {
ginCtx.Writer.WriteHeader(http.StatusInternalServerError)
return
}
return
}
response := dto.LoginResponse{
AccessToken: *jwtString,
}
ginCtx.JSON(http.StatusOK, response)
}
}
func (c *PlainAuthController) Method() string {
return http.MethodPost
}
func (c *PlainAuthController) Path() string {
return "/api/v1/auth"
}
func (*PlainAuthController) New() controller.Controller {
return &PlainAuthController{
authModule: auth.New(),
}
}