Packages
middleware
Fiber middleware for JWT authentication, RBAC role checking, and CORS. All handlers work with contract.Token — swap the JWT adapter without changing routes.
JWT Auth
import "github.com/BounkhongDev/bkgo/middleware"
token := jwt.New(cfg.JWT) // contract.Token
// Protect a route group
api := app.Group("/api/v1", middleware.JWT(token))
// Read claims inside any protected handler
func (h *Handler) Get(c *fiber.Ctx) error {
claims := middleware.Claims(c)
userID := claims["user_id"].(string)
role := claims["role"].(string)
// ...
}Returns 401 if the token is missing, expired, or has an invalid signature. Claims are stored in c.Locals for downstream handlers.
RBAC — RequireRole
// Single role
api.Delete("/users/:id",
middleware.RequireRole("admin"),
handler.Delete,
)
// Multiple allowed roles
api.Get("/reports",
middleware.RequireRole("admin", "manager"),
handler.ListReports,
)Reads claims["role"] set by the JWT middleware. Returns 403 if the role does not match.
CORS
// Permissive defaults (AllowOrigins: "*")
app.Use(middleware.CORS())
// Restrict to specific origin
app.Use(middleware.CORS(middleware.CORSConfig{
AllowOrigins: "https://myapp.com",
AllowHeaders: "Content-Type, Authorization",
AllowMethods: "GET, POST, PUT, DELETE, OPTIONS",
}))