53 lines
967 B
Go
53 lines
967 B
Go
package user
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"go.neonxp.ru/framework/pkg/tpl"
|
|
"go.neonxp.ru/framework/pkg/utils"
|
|
)
|
|
|
|
const oneyear = 86400 * 365
|
|
|
|
func (h *Handler) LoginForm(c echo.Context) error {
|
|
form := &tpl.LoginForm{}
|
|
if err := c.Bind(form); err != nil {
|
|
return err
|
|
}
|
|
|
|
if c.Request().Method == http.MethodPost {
|
|
err := h.doLogin(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.Login(form).
|
|
Render(c.Request().Context(), c.Response())
|
|
}
|
|
|
|
func (h *Handler) doLogin(c echo.Context, form *tpl.LoginForm) error {
|
|
u, err := h.user.Login(c.Request().Context(), form)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
maxage := 0
|
|
if form.Remember == "on" {
|
|
maxage = oneyear
|
|
}
|
|
|
|
return utils.SetUser(c, u, maxage)
|
|
}
|