feat: Added basic user authorization
This commit is contained in:
63
router/controller/auth/controller.go
Normal file
63
router/controller/auth/controller.go
Normal file
@@ -0,0 +1,63 @@
|
||||
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(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
|
||||
}
|
||||
Reference in New Issue
Block a user