hub apis
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user