44 lines
908 B
Go
44 lines
908 B
Go
package middleware
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/golang-jwt/jwt/v5"
|
|
)
|
|
|
|
func AuthMiddleware(c *fiber.Ctx) error {
|
|
tokenString := c.Get("Authorization")
|
|
tokenString = strings.TrimPrefix(tokenString, "Bearer ")
|
|
|
|
if tokenString == "" {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"message": "Missing or invalid token",
|
|
})
|
|
}
|
|
|
|
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
|
return []byte(os.Getenv("API_KEY")), nil
|
|
})
|
|
|
|
if err != nil || !token.Valid {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"message": "Invalid or expired token",
|
|
})
|
|
}
|
|
|
|
claims, ok := token.Claims.(jwt.MapClaims)
|
|
if !ok {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"message": "Invalid token claims",
|
|
})
|
|
}
|
|
|
|
userID := claims["sub"].(string)
|
|
|
|
c.Locals("userID", userID)
|
|
|
|
return c.Next()
|
|
}
|