You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I believe that all contrib middlewares that use the Fiber Context and allow the consumer to configure a custom context key must define the key to be of type interface{}.
Rationale
Using custom types for context keys is encouraged and common practice, both when using the standard Go context.Context and the Fiber fiber.Ctx.Locals function (see Fiber docs example on using Locals).
However, some contrib middlewares that rely on Fiber Context use a string key, forcing the consumer to go against standard practice.
Affected Middlewares
To my understanding, only the JWT and Paseto middlewares would require this change.
Contribution
I'm more than happy to make a PR for this if we agree on this change. 😃
Additional Context (optional)
No response
Code Snippet (optional)
package main
import (
jwtware "github.com/gofiber/contrib/jwt""github.com/gofiber/fiber/v2""github.com/golang-jwt/jwt/v5"
)
typecustomCtxKeyTypestruct{}
varctxKeyUser=customCtxKeyType{}
funcmain() {
app:=fiber.New()
app.Use(jwtware.New(jwtware.Config{
ContextKey: ctxKeyUser// <-- Error: Cannot use 'ctxKeyUser' (type customCtxKeyType) as the type string
}))
app.Get("/foo", func(c*fiber.Ctx) error {
userToken:=c.Locals(ctxKeyUser).(*jwt.Token) // <-- OK
})
}
Feature Description
I believe that all contrib middlewares that use the Fiber Context and allow the consumer to configure a custom context key must define the key to be of type
interface{}
.Rationale
Using custom types for context keys is encouraged and common practice, both when using the standard Go
context.Context
and the Fiberfiber.Ctx.Locals
function (see Fiber docs example on using Locals).However, some contrib middlewares that rely on Fiber Context use a
string
key, forcing the consumer to go against standard practice.Affected Middlewares
To my understanding, only the JWT and Paseto middlewares would require this change.
Contribution
I'm more than happy to make a PR for this if we agree on this change. 😃
Additional Context (optional)
No response
Code Snippet (optional)
Checklist:
The text was updated successfully, but these errors were encountered: