Skip to content

KeyAuthWithConfig - how to call next handler with decoded user #2114

Answered by aldas
fundef1 asked this question in Q&A
Discussion options

You must be logged in to vote

You can use echo.Context.Set and echo.Context.Get to store value in middleware into context and get that value from context in downstream middlewares or handler

const MyValueContextKey = "my-context-value"

func main() {
	e := echo.New()

	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			value := c.QueryParam("value")
			if value != "" {
				c.Set(MyValueContextKey, value) // store in context
			}
			return next(c)
		}
	})

	e.GET("/", func(c echo.Context) error {
		value, ok := c.Get(MyValueContextKey).(string) // get from context
		if !ok {
			value = "no value in context"
		}
		return c.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@fundef1
Comment options

Answer selected by fundef1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants