diff --git a/main.go b/main.go index 2902f29..d60e74a 100644 --- a/main.go +++ b/main.go @@ -177,20 +177,8 @@ func JWTAuthMiddleware() gin.HandlerFunc { return proto.SigningKey, nil }) // 错误处理 - if err != nil { - if ve, ok := err.(*jwt.ValidationError); ok { - switch { - case ve.Errors&jwt.ValidationErrorMalformed != 0: - c.AbortWithStatusJSON(http.StatusOK, gin.H{"error": "Malformed token", "code": proto.TokenInvalid}) - case ve.Errors&jwt.ValidationErrorExpired != 0: - c.AbortWithStatusJSON(http.StatusOK, gin.H{"error": "Token expired", "code": proto.TokenExpired}) - case ve.Errors&jwt.ValidationErrorNotValidYet != 0: - c.AbortWithStatusJSON(http.StatusOK, gin.H{"error": "Token not active yet", "code": proto.TokenInvalid}) - default: - c.AbortWithStatusJSON(http.StatusOK, gin.H{"error": "Invalid token", "code": proto.TokenInvalid}) - } - return - } + if err != nil || token.Valid == false { + c.AbortWithStatusJSON(http.StatusOK, gin.H{"code": proto.TokenInvalid, "message": "unauthorized", "error": "token is invalid:" + err.Error()}) } // 将用户信息添加到上下文中