-
Notifications
You must be signed in to change notification settings - Fork 12
/
email_verification.go
90 lines (77 loc) · 2.67 KB
/
email_verification.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
package makeless_go
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/makeless/makeless-go/http"
"github.com/makeless/makeless-go/mailer"
"github.com/makeless/makeless-go/model"
"gorm.io/gorm"
h "net/http"
"sync"
)
func (makeless *Makeless) verifyEmailVerification(http makeless_go_http.Http) error {
http.GetRouter().GetEngine().PATCH(
"/api/email-verification/verify",
func(c *gin.Context) {
var err error
var token = c.Query("token")
var emailVerification = &makeless_go_model.EmailVerification{
RWMutex: new(sync.RWMutex),
}
if emailVerification, err = http.GetDatabase().GetEmailVerificationByField(http.GetDatabase().GetConnection().WithContext(c), emailVerification, "token", token); err != nil {
switch errors.Is(err, gorm.ErrRecordNotFound) {
case true:
c.AbortWithStatusJSON(h.StatusBadRequest, http.Response(err, nil))
default:
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
}
return
}
if emailVerification, err = http.GetDatabase().VerifyEmailVerification(http.GetDatabase().GetConnection().WithContext(c), emailVerification); err != nil {
c.AbortWithStatusJSON(h.StatusInternalServerError, http.Response(err, nil))
return
}
c.JSON(h.StatusOK, http.Response(nil, emailVerification))
},
)
return nil
}
func (makeless *Makeless) resendEmailVerification(http makeless_go_http.Http) error {
http.GetRouter().GetEngine().POST(
"/api/auth/email-verification/resend",
http.GetAuthenticator().GetMiddleware().MiddlewareFunc(),
func(c *gin.Context) {
var err error
var mail makeless_go_mailer.Mail
var userId = http.GetAuthenticator().GetAuthUserId(c)
var user = &makeless_go_model.User{
Model: makeless_go_model.Model{Id: userId},
RWMutex: new(sync.RWMutex),
}
if user, err = http.GetDatabase().GetUserByField(http.GetDatabase().GetConnection().WithContext(c), user, "id", fmt.Sprintf("%d", user.GetId())); 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,
},
makeless.GetConfig().GetConfiguration().GetLocale(),
); 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
}