Update JWT

This commit is contained in:
2025-06-27 11:17:20 +07:00
parent 54aeb3568a
commit 1db200435e
12 changed files with 110 additions and 32 deletions
+14 -3
View File
@@ -12,7 +12,9 @@ func JWTProtected() fiber.Handler {
return func(c *fiber.Ctx) error {
authHeader := c.Get("Authorization")
if authHeader == "" {
return c.SendStatus(fiber.StatusUnauthorized)
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"message": "Missing Authorization header",
})
}
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
@@ -21,10 +23,19 @@ func JWTProtected() fiber.Handler {
})
if err != nil || !token.Valid {
return c.SendStatus(fiber.StatusUnauthorized)
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"message": "Invalid or expired token",
})
}
c.Locals("user", token.Claims)
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"message": "Invalid token claims",
})
}
c.Locals("user", claims)
return c.Next()
}
}