Locals

Method that stores string variables scoped to the request and therefore available only to the routes that match the request.

This is useful, if you want to pass some specific data to the next middleware.
  1. c.Locals(key string, value ...interface{}) interface{}
  1. app.Get("/", func(c *fiber.Ctx) {
  2. c.Locals("user", "admin")
  3. c.Next()
  4. })
  5.  
  6. app.Get("/admin", func(c *fiber.Ctx) {
  7. if c.Locals("user") == "admin" {
  8. c.Status(200).Send("Welcome, admin!")
  9. } else {
  10. c.SendStatus(403)
  11. // => 403 Forbidden
  12. }
  13. })