token_middleware.go 638 B

123456789101112131415161718192021222324
  1. package middleware
  2. import (
  3. "Ic_ouath/app/e"
  4. "Ic_ouath/utils"
  5. "github.com/gin-gonic/gin"
  6. )
  7. func Authentication() gin.HandlerFunc {
  8. return func(c *gin.Context) {
  9. header := c.GetHeader("Authorization")
  10. token, err := utils.ParseToken(header)
  11. if err != e.SUCCESS {
  12. e.ResponseWithMsg(c, e.TokenIsInvalid, e.TokenIsInvalid.GetMsg())
  13. c.Abort()
  14. return
  15. } else if token.Role != "admin" {
  16. e.ResponseWithMsg(c, e.TokenIsInvalid, e.TokenIsInvalid.GetMsg())
  17. c.Abort()
  18. }
  19. c.Set("user_id", token.UserId)
  20. c.Next()
  21. }
  22. }