🤔 How do I Set/Get data in context?
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")
}
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)
}
app.Use(func() fiber.Handler {
return func(c *fiber.Ctx) error {
return c.Next()
}
}())
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)
}
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!
https://docs.gofiber.io/api/ctx#locals
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.
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.
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)