123456789101112131415161718192021222324252627282930 |
- package middlewares
- import (
- "github.com/bytedance/sonic"
- "github.com/gin-gonic/gin"
- "lot_interlligentControl/app/e"
- "lot_interlligentControl/nats"
- "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()
- }
- }
- }
- }
|