diff options
author | Alexander NeonXP Kiryukhin <i@neonxp.ru> | 2024-07-20 21:56:37 +0300 |
---|---|---|
committer | Alexander NeonXP Kiryukhin <i@neonxp.ru> | 2024-07-20 21:58:27 +0300 |
commit | e9a64f3b41b5eae47dec7c0ecfd1caae83136abc (patch) | |
tree | c07898520a7015c3f2a201ca0674efea82dd76a5 /middleware | |
parent | 9c0a6b21f8f8d9b725e1fe7f11da4532e04fd9f1 (diff) |
initialv0.0.1
Diffstat (limited to 'middleware')
-rw-r--r-- | middleware/user.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/middleware/user.go b/middleware/user.go new file mode 100644 index 0000000..eaca6e9 --- /dev/null +++ b/middleware/user.go @@ -0,0 +1,34 @@ +package middleware + +import ( + "context" + + "github.com/labstack/echo-contrib/session" + "github.com/labstack/echo/v4" + "gitrepo.ru/neonxp/gorum/contextlib" + "gitrepo.ru/neonxp/gorum/models" +) + +func UserMiddleware() echo.MiddlewareFunc { + return func(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + sess, err := session.Get("session", c) + if err != nil { + return err + } + u, okSess := sess.Values["user"] + if !okSess { + return next(c) + } + user, okUser := u.(models.User) + if !okUser { + return next(c) + } + ctx := context.WithValue(c.Request().Context(), contextlib.UserKey, user) + req := c.Request().WithContext(ctx) + c.SetRequest(req) + + return next(c) + } + } +} |