64 lines
1.4 KiB
Go
64 lines
1.4 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/go-andiamo/chioas"
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/render"
|
|
)
|
|
|
|
type Controller struct {
|
|
authModule auth.Module
|
|
}
|
|
|
|
func (*Controller) New() controller.Controller {
|
|
return &Controller{
|
|
authModule: auth.New(),
|
|
}
|
|
}
|
|
|
|
func (c *Controller) plainLogin(w http.ResponseWriter, r *http.Request) {
|
|
var payload dto.LoginRequest
|
|
|
|
if err := render.Bind(r, &payload); err != nil {
|
|
sendErr := httpApp.NewResponseErrBuilder().WithStatusCode(http.StatusBadRequest).WithMessage(err.Error()).Send(w, r)
|
|
if sendErr != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
jwtString, err := c.authModule.Proceed(r.Context(), payload.Login, payload.Password)
|
|
if err != nil {
|
|
sendErr := httpApp.NewResponseErrBuilder().WithStatusCode(http.StatusBadRequest).WithMessage(err.Error()).Send(w, r)
|
|
if sendErr != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
response := dto.LoginResponse{
|
|
AccessToken: *jwtString,
|
|
}
|
|
|
|
render.JSON(w, r, response)
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
|
|
func (c *Controller) Group(r chi.Router) {
|
|
r.Post("/auth/plain", c.plainLogin)
|
|
}
|
|
|
|
func (c *Controller) Documentate() (*chioas.Paths, *chioas.Components) {
|
|
return nil, nil
|
|
}
|