generated from uberswe/golang-base-project
/
resendactivation.go
52 lines (47 loc) · 1.67 KB
/
resendactivation.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
package routes
import (
"github.com/ahamtat/go-url-shortener/models"
"github.com/gin-gonic/gin"
"log"
"net/http"
)
func (controller Controller) ResendActivation(c *gin.Context) {
pd := PageData{
Title: "Resend Activation Email",
IsAuthenticated: isAuthenticated(c),
CacheParameter: controller.config.CacheParameter,
}
c.HTML(http.StatusOK, "resendactivation.html", pd)
}
func (controller Controller) ResendActivationPost(c *gin.Context) {
pd := PageData{
Title: "Resend Activation Email",
IsAuthenticated: isAuthenticated(c),
CacheParameter: controller.config.CacheParameter,
}
email := c.PostForm("email")
user := models.User{Email: email}
res := controller.db.Where(&user).First(&user)
if res.Error == nil && user.ActivatedAt == nil {
activationToken := models.Token{
Type: models.TokenUserActivation,
ModelID: int(user.ID),
}
res = controller.db.Where(&activationToken).First(&activationToken)
if res.Error == nil {
// If the activation token exists we simply send an email
go controller.sendActivationEmail(activationToken.Value, user.Email)
} else {
// If there is no token then we need to generate a new token
go controller.activationEmailHandler(user.ID, user.Email)
}
} else {
log.Println(res.Error)
}
// We always return a positive response here to prevent user enumeration and other attacks
pd.Messages = append(pd.Messages, Message{
Type: "success",
Content: "A new activation email has been sent if the account exists and is not already activated. Please remember to check your spam inbox in case the email is not showing in your inbox.",
})
c.HTML(http.StatusOK, "resendactivation.html", pd)
}