Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀 [Feature]: Use type interface{} for context key config properties #1232

Open
3 tasks done
matei-radu opened this issue Feb 21, 2025 · 0 comments
Open
3 tasks done
Labels

Comments

@matei-radu
Copy link

matei-radu commented Feb 21, 2025

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 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"
)

type customCtxKeyType struct{}
var ctxKeyUser = customCtxKeyType{}

func main() {
    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
    })
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my suggestion prior to opening this one.
  • I understand that improperly formatted feature requests may be closed without explanation.
@matei-radu matei-radu added the ✏️ Feature New feature or request label Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants