framework/pkg/handler/user/register.go

46 lines
895 B
Go
Raw Normal View History

2024-10-12 02:52:22 +03:00
package user
import (
"net/http"
"github.com/labstack/echo/v4"
"go.neonxp.ru/framework/pkg/tpl"
"go.neonxp.ru/framework/pkg/utils"
)
func (h *Handler) RegisterForm(c echo.Context) error {
form := &tpl.RegisterForm{}
if err := c.Bind(form); err != nil {
return err
}
if c.Request().Method == http.MethodPost {
err := h.doRegister(c, form)
if err == nil {
if utils.IsHTMX(c) {
utils.HTMXRedirect(c, "/")
return c.NoContent(http.StatusNoContent)
}
return c.Redirect(http.StatusFound, "/")
}
form.Message = UserErrors.Get(err)
if form.Message == "" {
return err
}
}
return tpl.Register(form).
Render(c.Request().Context(), c.Response())
}
func (h *Handler) doRegister(c echo.Context, form *tpl.RegisterForm) error {
u, err := h.user.Register(c.Request().Context(), form)
if err != nil {
return err
}
return utils.SetUser(c, u, oneyear)
}