fiber icon indicating copy to clipboard operation
fiber copied to clipboard

🤔 How do I Set/Get data in context?

Open hyingreborn opened this issue 6 years ago • 8 comments

Question description I want save the user information in JWT middleware and get it in the routing handler

Code snippet (optional) In gin,i'll do it like this:

main.go

package main

func main() {
  app := fiber.New()
  app.Use("/api", midlleware.JwtAuth)
  app.Get("/api/v1/manager", api.ManagerGet)
}

middleware/jwt.go

package middleware

func JwtAuth() {
  // parse authorization
  c.Set("claims", claims)
  c.Next()
}

api/v1/manager.go

package api

func ManagerGet(c *fiber.Ctx) {
  claims, err := c.Get("claims")
}

hyingreborn avatar Feb 20 '20 04:02 hyingreborn

We use Locals, for more information please visit our documentation.

func main() {
  app := fiber.New()
  app.Use("/api", func(c *fiber.Ctx) {
    c.Locals("claims", claims)
    c.Next()
  })
  app.Get("/api/v1/manager", func(c *fiber.Ctx) {
    claims := c.Locals("claims")
  })
  app.Listen(3000)
}

Fenny avatar Feb 20 '20 04:02 Fenny

	app.Use(func() fiber.Handler {
		return func(c *fiber.Ctx) error {

			return c.Next()
		}
	}())

shubhamdixit863 avatar Jan 16 '22 17:01 shubhamdixit863

I am new in fiber. How can we reach claims im 4th line

func main() {
  app := fiber.New()
  app.Use("/api", func(c *fiber.Ctx) {
    c.Locals("claims", claims) --> how can we reach claims in here 
    c.Next()
  })
  app.Get("/api/v1/manager", func(c *fiber.Ctx) {
    claims := c.Locals("claims")
  })
  app.Listen(3000)
}

levniko avatar Aug 06 '22 16:08 levniko

This is an awesome feature in the Context - might want to list it as a feature elsewhere so this isn't the route to find it!

acidjazz avatar Sep 21 '22 02:09 acidjazz

https://docs.gofiber.io/api/ctx#locals

ReneWerner87 avatar Sep 21 '22 06:09 ReneWerner87

We use Locals, for more information please visit our documentation.

func main() {
  app := fiber.New()
  app.Use("/api", func(c *fiber.Ctx) {
    c.Locals("claims", claims)
    c.Next()
  })
  app.Get("/api/v1/manager", func(c *fiber.Ctx) {
    claims := c.Locals("claims")
  })
  app.Listen(3000)
}

How can this work c.Locals get two parametr how you could give him one and publish here that this is working.

martirosharutyunyan avatar Feb 20 '24 16:02 martirosharutyunyan

We use Locals, for more information please visit our documentation.

func main() {
  app := fiber.New()
  app.Use("/api", func(c *fiber.Ctx) {
    c.Locals("claims", claims)
    c.Next()
  })
  app.Get("/api/v1/manager", func(c *fiber.Ctx) {
    claims := c.Locals("claims")
  })
  app.Listen(3000)
}

How can this work c.Locals get two parametr how you could give him one and publish here that this is working.

This method allows you to store and retrieve any data in the context of a request. It can accept one or two parameters, depending on whether you want to get or set a value. The first parameter is the key, which is any type that can be used as a map key. The second parameter is optional, and it is the value that you want to associate with the key. If you omit the second parameter, the method will return the value that is stored for the key, or nil if there is none. If you provide the second parameter, the method will set the value for the key and return it.

You can see why in the current func implementation:

func (c *DefaultCtx) Locals(key any, value ...any) any {
	if len(value) == 0 {
		return c.fasthttp.UserValue(key)
	}
	c.fasthttp.SetUserValue(key, value[0])
	return value[0]
}

hope that helps.

sixcolors avatar Feb 20 '24 16:02 sixcolors

https://docs.gofiber.io/api/ctx#locals

I know this issue closed. Just for the future me and everyone else, this works for me. Pass this data to the controllers function once pass the middleware.

middleware

c.Locals("hello", user)

return c.Next()

controllers

user := c.Locals("hello")
log.Println("user", user)

aznj avatar May 23 '24 11:05 aznj