-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
55 lines (48 loc) · 1.02 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package middleware
import (
"github.com/fighthorse/sampleBookReader/interface/api/service"
"github.com/gin-gonic/gin"
)
func AuthRequired(c *gin.Context) {
}
type TokenReq struct {
Token string `form:"token" json:"token"` // token有效
}
func GuestRequired(c *gin.Context) {
token := c.Query("token")
if token == "" {
token = c.PostForm("token")
}
if token == "" {
return
}
// 解析token
data, err := service.LoginService.Check(c, token)
if err != nil {
return
}
c.Set("user-info", data)
c.Next()
}
func TokenRequired(c *gin.Context) {
token := c.Query("token")
if token == "" {
token = c.PostForm("token")
}
if token == "" {
c.JSON(200, gin.H{"code": -126, "message": "需要登录", "data": map[string]interface{}{}})
c.Abort()
return
}
// 解析token
data, err := service.LoginService.Check(c, token)
if err != nil {
c.JSON(200, gin.H{"code": -126, "message": err.Error(), "data": map[string]interface{}{
"token": token,
}})
c.Abort()
return
}
c.Set("user_info", data)
c.Next()
}