Skip to content

📝 [Proposal]: Access ctx from Authorizer function from BasicAuth middleware. #3095

@nakisen

Description

@nakisen

Feature Proposal Description

I needed to access ctx from the Authorizer function. I make a database query in this function and I needed to transfer some values ​​to the main handler. Since ctx cannot be accessed, a DB query needs to be made again in the main handler. I somehow solved my need, but if this becomes a permanent feature, it would be useful for different usage scenarios.

Alignment with Express API

This can be done in Express.js.

HTTP RFC Standards Compliance

This is not an issue related to the http RFC.

API Stability

A feature that could be used in the future.

Feature Examples

func initHttpServer() error {
...
	basicAuthCfg := basicauth.Config{
		Authorizer:   authorizeHttpBasic,
	}
	basicAuthMiddleware := basicauth.New(basicAuthCfg)
	httpSrv.Use("/pb", basicAuthMiddleware)

	httpSrv.Get("/pb/:brand", brand_xml)
...
}

func authorizeHttpBasic(user, pass string, c fiber.Ctx) bool {
	list := chekUserAndGetListFromDB(user, pass)
	if len(list) > 0 {
		c.Locals("list", list)
		return true
	}

	return false
}

func brand_xml(c fiber.Ctx) error {
...
	brand := c.Params("brand")
	list := c.Locals("list").([]map[string]string)
	return tmpl.ExecuteTemplate(c.Response().BodyWriter(), brand, list)
...
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have searched for existing issues that describe my proposal before opening this one.
  • I understand that a proposal that does not meet these guidelines may be closed without explanation.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions