24 lines
574 B
Go
24 lines
574 B
Go
package middleware
|
|
|
|
import (
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/storage/redis/v3"
|
|
)
|
|
|
|
func AuthMiddleware(redisClient *redis.Storage) fiber.Handler {
|
|
return func(c *fiber.Ctx) error {
|
|
token := c.Get("Authorization")
|
|
if token == "" {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Missing authorization token"})
|
|
}
|
|
|
|
userID, err := redisClient.Get(token)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Invalid or expired token"})
|
|
}
|
|
|
|
c.Locals("user_id", userID)
|
|
return c.Next()
|
|
}
|
|
}
|