package middlewares import ( "Panel_development/app/e" "Panel_development/nats" "github.com/bytedance/sonic" "github.com/gin-gonic/gin" "time" ) func AdminMiddleware() gin.HandlerFunc { return func(c *gin.Context) { header := c.GetHeader("Authorization") request, err := nats.Nats.Request("login_token_validation", []byte(header), 3*time.Second) if err != nil { e.ResponseWithMsg(c, e.TokenIsInvalid, e.TokenIsInvalid.GetMsg()) } else { var response UserResponse sonic.Unmarshal(request.Data, &response) users := response.User.(map[string]interface{}) role := users["role"].(string) if role == "admin" { c.Next() } else { e.ResponseWithMsg(c, e.TokenIsInvalid, e.TokenIsInvalid.GetMsg()) c.Abort() } } } }