You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
819 B

package middleware
import (
"github.com/gin-gonic/gin"
"live/app/lib/recook"
"strconv"
)
/*
令牌校验 可登录可不登录
*/
func BothAuth() gin.HandlerFunc {
return func(c *gin.Context) {
if c.Request.Method == "GET" {
c.Next()
} else if c.Request.Method == "POST" {
if len(c.Request.Header.Get("X-Recook-ID")) == 0 {
return
}
if len(c.Request.Header.Get("X-Recook-Token")) == 0 {
return
}
id, err := strconv.ParseUint(c.Request.Header.Get("X-Recook-ID"), 10, 64)
if id <= 0 || err != nil {
return
}
deviceType := c.Request.Header.Get("Device-Type")
token := c.Request.Header.Get("X-Recook-Token")
userId := recook.User.GetUserId(uint(id), token, deviceType)
if userId == 0 {
return
}
c.Set("userId", userId)
} else {
c.Abort()
}
}
}