forked from makeless/makeless-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
password_request.go
78 lines (66 loc) · 2.2 KB
/
password_request.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
package makeless_go
import (
"github.com/gin-gonic/gin"
"github.com/makeless/makeless-go/http"
"github.com/makeless/makeless-go/mailer"
"github.com/makeless/makeless-go/model"
"github.com/makeless/makeless-go/struct"
h "net/http"
"sync"
"time"
)
func (makeless *Makeless) passwordRequest(http makeless_go_http.Http) error {
http.GetRouter().GetEngine().POST(
"/api/password-request",
func(c *gin.Context) {
var err error
var userExists bool
var token string
var mail makeless_go_mailer.Mail
var tokenExpire = time.Now().Add(time.Hour * 1)
var tokenUsed = false
var tmpPasswordRequest = &_struct.PasswordRequest{
RWMutex: new(sync.RWMutex),
}
if err = c.ShouldBind(tmpPasswordRequest); err != nil {
c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(err, nil))
return
}
if userExists, err = http.GetSecurity().UserExists(http.GetSecurity().GetDatabase().GetConnection(), "email", *tmpPasswordRequest.GetEmail()); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
if !userExists {
c.AbortWithStatusJSON(h.StatusOK, http.Response(nil, nil))
return
}
if token, err = http.GetSecurity().GenerateToken(32); err != nil {
c.AbortWithStatusJSON(h.StatusOK, http.Response(nil, nil))
return
}
var passwordRequest = &makeless_go_model.PasswordRequest{
Email: tmpPasswordRequest.GetEmail(),
Token: &token,
Expire: &tokenExpire,
Used: &tokenUsed,
RWMutex: new(sync.RWMutex),
}
if err = http.GetDatabase().CreatePasswordRequest(http.GetDatabase().GetConnection().WithContext(c), passwordRequest); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
if mail, err = http.GetMailer().GetMail("passwordRequest", map[string]interface{}{
"passwordRequest": passwordRequest,
}); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
if err = http.GetMailer().SendQueue(mail); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
c.JSON(h.StatusOK, http.Response(nil, nil))
},
)
return nil
}