package middleware import ( "context" "fmt" "github.com/golang-jwt/jwt/v4" echojwt "github.com/labstack/echo-jwt/v4" "github.com/labstack/echo/v4" ) func Jwt(parser func(c echo.Context, auth string) (interface{}, error)) echo.MiddlewareFunc { var ( sKeyJwtToken = fmt.Sprintf("%s", keyJwtToken) cfg = echojwt.Config{ ContinueOnIgnoredError: true, ErrorHandler: func(c echo.Context, err error) error { return nil }, ContextKey: sKeyJwtToken, ParseTokenFunc: parser, SuccessHandler: func(ctx echo.Context) { value := context.WithValue(ctx.Request().Context(), keyJwtToken, ctx.Get(sKeyJwtToken)) ctx.SetRequest(ctx.Request().WithContext(value)) }, } ) return echojwt.WithConfig(cfg) } func GetToken(ctx context.Context) (*jwt.Token, error) { t, ok := ctx.Value(keyJwtToken).(*jwt.Token) if !ok { return nil, fmt.Errorf("missing or malformed token") } return t, nil }