Update JWT
This commit is contained in:
+14
-3
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user