Skip to content

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

@matei-radu

Description

@matei-radu

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions