framework/pkg/middleware/user.go

29 lines
532 B
Go
Raw Permalink Normal View History

2024-10-12 02:52:22 +03:00
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)
}
}
}