1234567891011121314151617181920212223242526272829303132 |
- package middlewares
- import (
- "github.com/bytedance/sonic"
- "github.com/gin-gonic/gin"
- "project_management/app/e"
- "project_management/nats"
- "strings"
- "time"
- )
- func AdminMiddleware() gin.HandlerFunc {
- return func(c *gin.Context) {
- header := c.GetHeader("Authorization")
- split := strings.Split(header, ":")
- request, err := nats.Nats.Request("login_token_validation", []byte(split[1]), 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()
- }
- }
- }
- }
|