mirror of
https://github.com/get-drexa/drive.git
synced 2025-12-06 16:21:39 +00:00
refactor: introduce reqctx pkg for request context
This commit is contained in:
23
apps/backend/internal/reqctx/reqctx.go
Normal file
23
apps/backend/internal/reqctx/reqctx.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package reqctx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
const authenticatedUserKey = "authenticatedUser"
|
||||
|
||||
var ErrUnauthenticatedRequest = errors.New("unauthenticated request")
|
||||
|
||||
// AuthenticatedUser returns the authenticated user from the given fiber context.
|
||||
// Returns ErrUnauthenticatedRequest if not authenticated.
|
||||
// The caller must type assert the returned value to the appropriate user type.
|
||||
func AuthenticatedUser(c *fiber.Ctx) any {
|
||||
return c.Locals(authenticatedUserKey)
|
||||
}
|
||||
|
||||
// SetAuthenticatedUser sets the authenticated user in the fiber context.
|
||||
func SetAuthenticatedUser(c *fiber.Ctx, user interface{}) {
|
||||
c.Locals(authenticatedUserKey, user)
|
||||
}
|
||||
Reference in New Issue
Block a user