Skip to content

Instantly share code, notes, and snippets.

@mr-pascal
Last active February 27, 2021 14:56
Show Gist options
  • Select an option

  • Save mr-pascal/299fe811ef91473077ae9d172e7b6323 to your computer and use it in GitHub Desktop.

Select an option

Save mr-pascal/299fe811ef91473077ae9d172e7b6323 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/gofiber/fiber/v2"
jwtware "github.com/gofiber/jwt/v2"
"log"
)
func main() {
// Create new Go Fiber application web server
app := fiber.New()
// Use YOUR 'HS256' signing key here
signingKey := "YOUR_SIGNING_KEY"
// A unrestricted 'ping' route
unrestrictedPingPath := "/ping/unrestricted"
// List of unrestricted paths
unrestrictedPaths := map[string]bool{
unrestrictedPingPath: true,
}
// JWT Middleware
app.Use(jwtware.New(jwtware.Config{
SigningKey: []byte(signingKey),
SigningMethod: "HS256",
Filter: func(ctx *fiber.Ctx) bool {
currentPath := ctx.Path()
v, _ := unrestrictedPaths[currentPath]
// If 'true' the JWT middleware is ignored
return v
},
}))
// Add route handlers
app.Get("/ping", MyHandler)
app.Get(unrestrictedPingPath, MyHandler)
// Start server
log.Fatal(app.Listen(":8080"))
}
func MyHandler(c *fiber.Ctx) error {
return c.SendString("Works")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment