framework/pkg/middleware/user.go

28 lines
532 B
Go

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)
}
}
}