diff --git a/backend/auth/jwt.go b/backend/auth/jwt.go index c0c52a7c7..d2a2c7d94 100644 --- a/backend/auth/jwt.go +++ b/backend/auth/jwt.go @@ -18,10 +18,14 @@ type CustomClaims struct { Role string `json:"role"` } +type localsKey byte + +const claimsKey localsKey = 0 + // From extracts the CustomClaims from the fiber context // Returns nil if the claims are not present func From(c *fiber.Ctx) (*CustomClaims, error) { - rawClaims := c.Locals("claims") + rawClaims := c.Locals(claimsKey) if rawClaims == nil { return nil, utilities.Forbidden() } @@ -34,6 +38,10 @@ func From(c *fiber.Ctx) (*CustomClaims, error) { return claims, nil } +func SetClaims(c *fiber.Ctx, claims *CustomClaims) { + c.Locals(claimsKey, claims) +} + type JWTType string const ( diff --git a/backend/middleware/auth/auth.go b/backend/middleware/auth/auth.go index 1a0962766..7313a74db 100644 --- a/backend/middleware/auth/auth.go +++ b/backend/middleware/auth/auth.go @@ -65,7 +65,7 @@ func (m *AuthMiddlewareService) Authenticate(c *fiber.Ctx) error { // return errors.Unauthorized.FiberError(c) // } - c.Locals("claims", claims) + auth.SetClaims(c, claims) return nil }(c)