Files
LingXi-CRM/server/middleware/jwt.go
T

30 lines
565 B
Go
Raw Normal View History

2022-11-28 16:38:30 +08:00
package middleware
import (
2022-12-11 20:02:19 +08:00
"crm/common"
2022-11-28 16:38:30 +08:00
"crm/response"
2022-12-11 20:02:19 +08:00
"strconv"
2022-11-28 16:38:30 +08:00
"github.com/gin-gonic/gin"
)
2022-12-11 20:02:19 +08:00
// JWT认证中间件
2022-11-28 16:38:30 +08:00
func JwtAuth() gin.HandlerFunc {
return func(c *gin.Context) {
2022-12-11 20:02:19 +08:00
uid, _ := strconv.Atoi(c.Request.Header.Get("uid"))
2022-11-28 16:38:30 +08:00
token := c.Request.Header.Get("token")
if token == "" {
response.Result(response.ErrCodeNoLogin, nil, c)
c.Abort()
return
}
2022-12-11 20:02:19 +08:00
userid, err := common.VerifyToken(token)
if userid != int64(uid) || err != nil {
2022-11-28 16:38:30 +08:00
response.Result(response.ErrCodeTokenExpire, nil, c)
c.Abort()
return
}
c.Next()
}
}