diff options
Diffstat (limited to 'middleware/user.go')
-rw-r--r-- | middleware/user.go | 38 |
1 files changed, 15 insertions, 23 deletions
diff --git a/middleware/user.go b/middleware/user.go index eaca6e9..f4eb12a 100644 --- a/middleware/user.go +++ b/middleware/user.go @@ -2,33 +2,25 @@ package middleware import ( "context" + "net/http" - "github.com/labstack/echo-contrib/session" - "github.com/labstack/echo/v4" "gitrepo.ru/neonxp/gorum/contextlib" - "gitrepo.ru/neonxp/gorum/models" + "go.neonxp.ru/mux" + "go.neonxp.ru/mux/middleware/session" ) -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 +func UserMiddleware() mux.Middleware { + return func(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + session := session.FromRequest(r) + user := (*session)["user"] + if user == nil { + h.ServeHTTP(w, r) + return } - 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) - } + h.ServeHTTP(w, r.WithContext( + context.WithValue(r.Context(), contextlib.UserKey, user), + )) + }) } } |