Skip to content

Group middlewares #178

@dre1080

Description

@dre1080

I'm currently having an issue with per-group middlewares. I'm not getting the desired outcome.

Here is some example code:

In server.go:

e := echo.New()
e.Use(...)
router.Setup(e)
e.Run(":1323")

In package router:

func Setup(e *echo.Echo) {
    auth := e.Group("/auth")
    auth.Use(func(c *echo.Context) {
       if c.Get("User") != nil {
           return echo.NewHTTPError(http.StatusForbidden, "Don't need User")
       }
       return nil
    })
    auth.Get("/login", myLoginHandler)

    user := e.Group("/user")
    user.Use(func(c *echo.Context) {
       if c.Get("User") == nil {
           return echo.NewHTTPError(http.StatusUnauthorized, "Need User")
       }
       return nil
    })
    user.Get("/logout", myLogoutHandler)
}

Now, when I start the server & navigate to /auth/login.
I get a "Need User" response when I shouldn't be getting any response & instead show the login page.

The same thing happens vice-versa, if I define user & its middleware before auth. I'll be able to logout.

It seems the middleware for the previous group is cancelled uses the next group's middleware instead.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions