gojwtgo-echo

Echo skip JWT middleware


I used golang jwt middleware.

e.Use(middleware.JWTWithConfig(middleware.JWTConfig{
  SigningKey:  []byte(os.Getenv("Signing_Key")),
  TokenLookup: "header:x-auth-token",       
}))

It waits token for all functions but I don't want to use this middleware for login function. How to prevent this?


Solution

  • There is a skipper function. You can use it to check which route to skip.

    JWTConfig struct {
      // Skipper defines a function to skip middleware.
      Skipper Skipper
      ... 
    }
    

    Check an example:

    e.Use(middleware.JWTWithConfig(middleware.JWTConfig{
        SigningKey:  []byte(os.Getenv("Signing_Key")),
        TokenLookup: "header:x-auth-token",
        Skipper: func(c echo.Context) bool {
           // Skip middleware if path is equal 'login'
           if c.Request().URL.Path == "/login" {
             return true
           }
           return false
        },
    }))