tam chat
This commit is contained in:
33
server/internal/middleware/staff.go
Normal file
33
server/internal/middleware/staff.go
Normal file
@@ -0,0 +1,33 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user