forked from makeless/makeless-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.go
82 lines (69 loc) · 2.32 KB
/
register.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
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/security"
"github.com/makeless/makeless-go/struct"
h "net/http"
"sync"
)
func (makeless *Makeless) register(http makeless_go_http.Http) error {
http.GetRouter().GetEngine().POST("/api/register", func(c *gin.Context) {
var err error
var token string
var userExists bool
var mail makeless_go_mailer.Mail
var verified = false
var register = &_struct.Register{
RWMutex: new(sync.RWMutex),
}
if err := c.ShouldBind(register); err != nil {
c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(err, nil))
return
}
if token, err = http.GetSecurity().GenerateToken(32); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
var user = &makeless_go_model.User{
Name: register.GetName(),
Password: register.GetPassword(),
Email: register.GetEmail(),
EmailVerification: &makeless_go_model.EmailVerification{
Token: &token,
Verified: &verified,
RWMutex: new(sync.RWMutex),
},
RWMutex: new(sync.RWMutex),
}
if userExists, err = http.GetSecurity().UserExists(http.GetSecurity().GetDatabase().GetConnection(), "email", *user.Email); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
if userExists {
c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(makeless_go_security.UserAlreadyExists, nil))
return
}
if user, err = http.GetSecurity().Register(http.GetSecurity().GetDatabase().GetConnection(), user); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
if user.GetEmailVerification().RWMutex == nil {
user.GetEmailVerification().RWMutex = new(sync.RWMutex)
}
if mail, err = http.GetMailer().GetMail("emailVerification", map[string]interface{}{
"user": user,
}); 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, user))
})
return nil
}