oakkose
oakkose

Reputation: 405

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?

Upvotes: 2

Views: 2871

Answers (2)

otaxhu
otaxhu

Reputation: 320

It could be something like this

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

Upvotes: 1

Roman Kiselenko
Roman Kiselenko

Reputation: 44360

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
    },
}))
 

Upvotes: 12

Related Questions