aboutsummaryrefslogtreecommitdiff
path: root/pkg/middleware/user.go
diff options
context:
space:
mode:
authorAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-10-12 02:52:22 +0300
committerAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-10-12 02:53:52 +0300
commitd05ea66f4bbcf0cc5c8908f3435c68de1b070fa1 (patch)
tree7c7a769206646f2b81a0eda0680f0be5033a4197 /pkg/middleware/user.go
Начальная версияv0.0.1
Diffstat (limited to 'pkg/middleware/user.go')
-rw-r--r--pkg/middleware/user.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/pkg/middleware/user.go b/pkg/middleware/user.go
new file mode 100644
index 0000000..6271e43
--- /dev/null
+++ b/pkg/middleware/user.go
@@ -0,0 +1,28 @@
+package middleware
+
+import (
+ "context"
+
+ "github.com/labstack/echo-contrib/session"
+ "github.com/labstack/echo/v4"
+)
+
+func User() echo.MiddlewareFunc {
+ return func(next echo.HandlerFunc) echo.HandlerFunc {
+ return func(c echo.Context) error {
+ sess, err := session.Get("user", c)
+ if err != nil {
+ return err
+ }
+
+ u := sess.Values["user"]
+ c.Set("user", u)
+
+ ctx := context.WithValue(c.Request().Context(), ContextKey("user"), u)
+ r := c.Request().WithContext(ctx)
+ c.SetRequest(r)
+
+ return next(c)
+ }
+ }
+}