admin_middleware.go 761 B

123456789101112131415161718192021222324252627282930
  1. package middlewares
  2. import (
  3. "Panel_development/app/e"
  4. "Panel_development/nats"
  5. "github.com/bytedance/sonic"
  6. "github.com/gin-gonic/gin"
  7. "time"
  8. )
  9. func AdminMiddleware() gin.HandlerFunc {
  10. return func(c *gin.Context) {
  11. header := c.GetHeader("Authorization")
  12. request, err := nats.Nats.Request("login_token_validation", []byte(header), 3*time.Second)
  13. if err != nil {
  14. e.ResponseWithMsg(c, e.TokenIsInvalid, e.TokenIsInvalid.GetMsg())
  15. } else {
  16. var response UserResponse
  17. sonic.Unmarshal(request.Data, &response)
  18. users := response.User.(map[string]interface{})
  19. role := users["role"].(string)
  20. if role == "admin" {
  21. c.Next()
  22. } else {
  23. e.ResponseWithMsg(c, e.TokenIsInvalid, e.TokenIsInvalid.GetMsg())
  24. c.Abort()
  25. }
  26. }
  27. }
  28. }