34 lines
799 B
Go
34 lines
799 B
Go
package middleware
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"server/internal/auth"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func RequireStaff() fiber.Handler {
|
|
return func(c *fiber.Ctx) error {
|
|
header := c.Get("Authorization")
|
|
if header == "" || !strings.HasPrefix(header, "Bearer ") {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Unauthorized"})
|
|
}
|
|
token := strings.TrimSpace(strings.TrimPrefix(header, "Bearer "))
|
|
claims, err := auth.ParseToken(token)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Invalid or expired token"})
|
|
}
|
|
c.Locals("staffId", claims.StaffID)
|
|
c.Locals("staffEmail", claims.Email)
|
|
return c.Next()
|
|
}
|
|
}
|
|
|
|
func StaffIDFromCtx(c *fiber.Ctx) uint {
|
|
if v, ok := c.Locals("staffId").(uint); ok {
|
|
return v
|
|
}
|
|
return 0
|
|
}
|