From d05ea66f4bbcf0cc5c8908f3435c68de1b070fa1 Mon Sep 17 00:00:00 2001 From: Alexander Neonxp Kiryukhin Date: Sat, 12 Oct 2024 02:52:22 +0300 Subject: Начальная версия MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/utils/htmx.go | 11 +++++++++++ pkg/utils/user.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 pkg/utils/htmx.go create mode 100644 pkg/utils/user.go (limited to 'pkg/utils') diff --git a/pkg/utils/htmx.go b/pkg/utils/htmx.go new file mode 100644 index 0000000..c4120f3 --- /dev/null +++ b/pkg/utils/htmx.go @@ -0,0 +1,11 @@ +package utils + +import "github.com/labstack/echo/v4" + +func IsHTMX(c echo.Context) bool { + return c.Request().Header.Get("HX-Request") == "true" +} + +func HTMXRedirect(c echo.Context, location string) { + c.Response().Header().Set("HX-Redirect", location) +} diff --git a/pkg/utils/user.go b/pkg/utils/user.go new file mode 100644 index 0000000..4006b3c --- /dev/null +++ b/pkg/utils/user.go @@ -0,0 +1,41 @@ +package utils + +import ( + "context" + + "github.com/gorilla/sessions" + "github.com/labstack/echo-contrib/session" + "github.com/labstack/echo/v4" + "go.neonxp.ru/framework/pkg/middleware" + "go.neonxp.ru/framework/pkg/model" +) + +func GetUserCtx(ctx context.Context) *model.User { + u := ctx.Value(middleware.ContextKey("user")) + if u == nil { + return nil + } + + if u, ok := u.(model.User); ok { + return &u + } + + return nil +} + +func SetUser(c echo.Context, u *model.User, maxage int) error { + sess, err := session.Get("user", c) + if err != nil { + return err + } + + sess.Values["user"] = u + sess.Options = &sessions.Options{ + Path: "/", + MaxAge: maxage, + HttpOnly: true, + Secure: true, + } + + return sess.Save(c.Request(), c.Response()) +} -- cgit v1.2.3