Skip to content

JWT graceful failure #1039

@coelho

Description

@coelho

Would be nice if the JWT middleware had a configuration option Required bool (or maybe an inverse variant for backwards compatibility, can't think of a name)?

Use-case is when you do not want a route to require a JWT, but display different behavior if a JWT is there.

Example:

func restricted(c echo.Context) error {
	user := c.Get("user")
	if user != nil {
		claims := user.(*jwt.Token).Claims.(*jwtCustomClaims)
		name := claims.Name
		return c.String(http.StatusOK, "Welcome "+name+"!")
	} else {
		return c.String(http.StatusOK, "Welcome anonymous!")
	}
}

Currently this would throw a ErrJWTMissing
https://github.com/labstack/echo/blob/master/middleware/jwt.go#L142
and
https://github.com/labstack/echo/blob/master/middleware/jwt.go#L158

Which would be the expected behavior if Required is true.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions