38 lines
730 B
Go
38 lines
730 B
Go
package gin
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func filterUser() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
token := c.GetHeader("Authorization")
|
|
if token == "" {
|
|
token = c.Query("Authorization")
|
|
}
|
|
|
|
staff, err := redis.UserGet(&token)
|
|
if err != nil {
|
|
logrus.WithFields(logrus.Fields{
|
|
"func": "authUser",
|
|
}).Errorf("redis.UserGet: %s", err.Error())
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
|
"data": "无效TOKEN, 请重新登录!",
|
|
})
|
|
return
|
|
}
|
|
|
|
if staff == nil {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
|
"data": "无效TOKEN, 请重新登录!",
|
|
})
|
|
return
|
|
}
|
|
c.Set("user", staff)
|
|
c.Next()
|
|
}
|
|
}
|