forked from makeless/makeless-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
108 lines (90 loc) · 3.12 KB
/
token.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package makeless_go
import (
"github.com/gin-gonic/gin"
"github.com/makeless/makeless-go/http"
"github.com/makeless/makeless-go/model"
"github.com/makeless/makeless-go/struct"
h "net/http"
"sync"
)
func (makeless *Makeless) tokens(http makeless_go_http.Http) error {
http.GetRouter().GetEngine().GET(
"/api/auth/token",
http.GetAuthenticator().GetMiddleware().MiddlewareFunc(),
http.EmailVerificationMiddleware(makeless.GetConfig().GetConfiguration().GetEmailVerification()),
func(c *gin.Context) {
userId := http.GetAuthenticator().GetAuthUserId(c)
var err error
var tokens []*makeless_go_model.Token
var user = &makeless_go_model.User{
Model: makeless_go_model.Model{Id: userId},
RWMutex: new(sync.RWMutex),
}
if tokens, err = http.GetDatabase().GetTokens(http.GetDatabase().GetConnection().WithContext(c), user, tokens); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
c.JSON(h.StatusOK, http.Response(nil, tokens))
},
)
return nil
}
func (makeless *Makeless) createToken(http makeless_go_http.Http) error {
http.GetRouter().GetEngine().POST(
"/api/auth/token",
http.GetAuthenticator().GetMiddleware().MiddlewareFunc(),
http.EmailVerificationMiddleware(makeless.GetConfig().GetConfiguration().GetEmailVerification()),
func(c *gin.Context) {
var err error
var userId = http.GetAuthenticator().GetAuthUserId(c)
var tokenCreate = &_struct.TokenCreate{
RWMutex: new(sync.RWMutex),
}
if err := c.ShouldBind(tokenCreate); err != nil {
c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(err, nil))
return
}
var token = &makeless_go_model.Token{
Note: tokenCreate.GetNote(),
Token: tokenCreate.GetToken(),
UserId: &userId,
RWMutex: new(sync.RWMutex),
}
if token, err = http.GetDatabase().CreateToken(http.GetDatabase().GetConnection().WithContext(c), token); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
c.JSON(h.StatusOK, http.Response(nil, token))
},
)
return nil
}
func (makeless *Makeless) deleteToken(http makeless_go_http.Http) error {
http.GetRouter().GetEngine().DELETE(
"/api/auth/token",
http.GetAuthenticator().GetMiddleware().MiddlewareFunc(),
http.EmailVerificationMiddleware(makeless.GetConfig().GetConfiguration().GetEmailVerification()),
func(c *gin.Context) {
var err error
var userId = http.GetAuthenticator().GetAuthUserId(c)
var tokenDelete = &_struct.TokenDelete{
RWMutex: new(sync.RWMutex),
}
if err := c.ShouldBind(tokenDelete); err != nil {
c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(err, nil))
return
}
var token = &makeless_go_model.Token{
Model: makeless_go_model.Model{Id: *tokenDelete.GetId()},
UserId: &userId,
RWMutex: new(sync.RWMutex),
}
if err = http.GetDatabase().DeleteToken(http.GetDatabase().GetConnection().WithContext(c), token); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
c.JSON(h.StatusOK, http.Response(nil, nil))
},
)
return nil
}