This commit is contained in:
2026-07-04 11:10:52 +05:30
parent bd6427f5db
commit c8adaf7815
18 changed files with 1140 additions and 92 deletions

View File

@@ -45,6 +45,50 @@ func AuthMiddleware(cfg *config.Config) fiber.Handler {
}
}
// HubStaffAuth validates the JWT and requires role 6 (hub staff), then
// exposes hubid via c.Locals so hub handlers can scope all queries to it.
func HubStaffAuth(cfg *config.Config) fiber.Handler {
return func(c *fiber.Ctx) error {
authHeader := c.Get("Authorization")
if authHeader == "" {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"success": false,
"message": "authorization header is required",
})
}
parts := strings.Split(authHeader, " ")
if len(parts) != 2 || parts[0] != "Bearer" {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"success": false,
"message": "authorization header must be in format: Bearer <token>",
})
}
claims, err := utils.ParseToken(parts[1], cfg.JWTSecret)
if err != nil {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"success": false,
"message": "invalid or expired token",
})
}
if claims.RoleID != 6 {
return c.Status(fiber.StatusForbidden).JSON(fiber.Map{
"success": false,
"message": "access restricted to hub staff",
})
}
c.Locals("userid", claims.UserID)
c.Locals("email", claims.Email)
c.Locals("roleid", claims.RoleID)
c.Locals("hubid", claims.HubID)
return c.Next()
}
}
func RoleCheckMiddleware(allowedRoles ...int) fiber.Handler {
return func(c *fiber.Ctx) error {
roleID, ok := c.Locals("roleid").(int)