background/controller/middle/auth.go

58 lines
1.1 KiB
Go
Raw Normal View History

/*
* @Author: your name
* @Date: 2020-10-04 18:06:06
* @LastEditTime: 2021-07-17 22:22:05
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \background\controller\middle\auth.go
*/
2019-03-27 07:03:45 +00:00
package middle
2019-04-07 04:25:07 +00:00
import (
"background/config"
"background/controller"
"background/model"
"encoding/json"
"log"
2020-09-26 04:30:23 +00:00
"github.com/gin-gonic/gin"
2019-04-07 04:25:07 +00:00
)
2019-03-27 07:03:45 +00:00
2020-09-26 04:30:23 +00:00
func AuthMiddle(c *gin.Context) {
2019-04-07 04:25:07 +00:00
token := c.Query("token")
user := c.Query("userid")
2020-09-26 04:30:23 +00:00
if user == "" || token == "" {
2019-09-06 07:53:04 +00:00
log.Print("error user not existed or token missing")
log.Print(c.Request.URL.String())
2020-09-26 04:30:23 +00:00
c.JSON(200, controller.RespBase{
"auth err", 20, nil,
2019-04-07 04:25:07 +00:00
})
c.Abort()
return
2019-04-07 04:25:07 +00:00
}
cli := config.RedisOne()
if cli != nil{
if cli.Exists(token).Val() {
users := model.Users{}
userInfo := config.RedisOne().Get(token).Val()
e := json.Unmarshal([]byte(userInfo), &users)
if nil != e {
c.JSON(200, controller.RespBase{
"auth err", 10, nil,
})
c.Abort()
return
}
} else {
2020-09-26 04:30:23 +00:00
c.JSON(200, controller.RespBase{
"expired or no login", 210, nil,
2019-04-07 04:25:07 +00:00
})
c.Abort()
return
2019-04-07 04:25:07 +00:00
}
c.Next()
2019-04-07 04:25:07 +00:00
}
2020-09-26 04:30:23 +00:00
}