aboutsummaryrefslogblamecommitdiff
path: root/pkg/handler/user/register.go
blob: 2bd810aa9718a5502b51c84db78175e244985a37 (plain) (tree)












































                                                                            
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)
}