aboutsummaryrefslogtreecommitdiff
path: root/pkg/handler/user
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/handler/user')
-rw-r--r--pkg/handler/user/errors.go30
-rw-r--r--pkg/handler/user/handler.go24
-rw-r--r--pkg/handler/user/login.go53
-rw-r--r--pkg/handler/user/logout.go18
-rw-r--r--pkg/handler/user/profile.go16
-rw-r--r--pkg/handler/user/register.go45
6 files changed, 186 insertions, 0 deletions
diff --git a/pkg/handler/user/errors.go b/pkg/handler/user/errors.go
new file mode 100644
index 0000000..ab8fc4a
--- /dev/null
+++ b/pkg/handler/user/errors.go
@@ -0,0 +1,30 @@
+package user
+
+import (
+ "errors"
+
+ "go.neonxp.ru/framework/pkg/service/user"
+)
+
+var UserErrors = Errors{
+ user.ErrInvalidUserOrPassword: "Неверный email или пароль",
+ user.ErrPasswordTooShort: "Пароль слишком короткий",
+ user.ErrPasswordTooWeak: "Пароль слишком простой",
+ user.ErrUserAlreadyExist: "Пользователь уже существует",
+ user.ErrUsernameToShort: "Имя пользователя слишком короткое",
+ // user.ErrEmailEmpty: "Электропочта не указана",
+ // user.ErrPasswordEmpty: "Пароль не указан",
+ // user.ErrNameEmpty: "Имя пользователя не указано",
+}
+
+type Errors map[error]string
+
+func (e Errors) Get(err error) string {
+ for target, msg := range e {
+ if errors.Is(err, target) {
+ return msg
+ }
+ }
+
+ return ""
+}
diff --git a/pkg/handler/user/handler.go b/pkg/handler/user/handler.go
new file mode 100644
index 0000000..6bc8bff
--- /dev/null
+++ b/pkg/handler/user/handler.go
@@ -0,0 +1,24 @@
+package user
+
+import (
+ "github.com/labstack/echo/v4"
+ "go.neonxp.ru/framework/pkg/service/user"
+)
+
+type Handler struct {
+ user *user.Service
+}
+
+// NewHandler returns new Handler.
+func NewHandler(u *user.Service) *Handler {
+ return &Handler{user: u}
+}
+
+func (h *Handler) Register(g *echo.Group) {
+ g.GET("/login", h.LoginForm)
+ g.POST("/login", h.LoginForm)
+ g.GET("/register", h.RegisterForm)
+ g.POST("/register", h.RegisterForm)
+ g.GET("/profile", h.Profile)
+ g.POST("/logout", h.Logout)
+}
diff --git a/pkg/handler/user/login.go b/pkg/handler/user/login.go
new file mode 100644
index 0000000..b3fbdbf
--- /dev/null
+++ b/pkg/handler/user/login.go
@@ -0,0 +1,53 @@
+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)
+}
diff --git a/pkg/handler/user/logout.go b/pkg/handler/user/logout.go
new file mode 100644
index 0000000..9b7ea5f
--- /dev/null
+++ b/pkg/handler/user/logout.go
@@ -0,0 +1,18 @@
+package user
+
+import (
+ "net/http"
+
+ "github.com/labstack/echo/v4"
+ "go.neonxp.ru/framework/pkg/utils"
+)
+
+func (*Handler) Logout(c echo.Context) error {
+ if err := utils.SetUser(c, nil, -1); err != nil {
+ return err
+ }
+
+ utils.HTMXRedirect(c, "/")
+
+ return c.NoContent(http.StatusNoContent)
+}
diff --git a/pkg/handler/user/profile.go b/pkg/handler/user/profile.go
new file mode 100644
index 0000000..7181ca7
--- /dev/null
+++ b/pkg/handler/user/profile.go
@@ -0,0 +1,16 @@
+package user
+
+import (
+ "github.com/labstack/echo/v4"
+ "go.neonxp.ru/framework/pkg/tpl"
+ "go.neonxp.ru/framework/pkg/utils"
+)
+
+func (*Handler) Profile(c echo.Context) error {
+ u := utils.GetUserCtx(c.Request().Context())
+ if u == nil {
+ return echo.ErrForbidden
+ }
+
+ return tpl.Profile(u).Render(c.Request().Context(), c.Response())
+}
diff --git a/pkg/handler/user/register.go b/pkg/handler/user/register.go
new file mode 100644
index 0000000..2bd810a
--- /dev/null
+++ b/pkg/handler/user/register.go
@@ -0,0 +1,45 @@
+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)
+}