package jwt import ( "github.com/dgrijalva/jwt-go" "github.com/gin-gonic/gin" "gin-server/lib/messages" "gin-server/security" "net/http" ) // JWT is jwt middleware func JWT() gin.HandlerFunc { return func(c *gin.Context) { var code int var data interface{} code = messages.SUCCESS token := c.Query("token") if token == "" { code = messages.INVALID_PARAMS } else { _, err := security.ParseToken(token) if err != nil { switch err.(*jwt.ValidationError).Errors { case jwt.ValidationErrorExpired: code = messages.ERROR_AUTH_CHECK_TOKEN_TIMEOUT default: code = messages.ERROR_AUTH_CHECK_TOKEN_FAIL } } } if code != messages.SUCCESS { c.JSON(http.StatusUnauthorized, gin.H{ "code": code, "msg": messages.GetMsg(code), "data": data, }) c.Abort() return } c.Next() } }