Archived
Template
1
0
This repository has been archived on 2023-12-20. You can view files and clone it, but cannot push or open issues or pull requests.
Files
golang-base-project/routes/resendactivation.go

49 lines
1.7 KiB
Go
Raw Normal View History

2021-12-12 14:56:13 +01:00
package routes
import (
"github.com/gin-gonic/gin"
"github.com/uberswe/golang-base-project/models"
"log"
"net/http"
)
// ResendActivation renders the HTML page used to request a new activation email
2021-12-12 14:56:13 +01:00
func (controller Controller) ResendActivation(c *gin.Context) {
2022-01-29 10:21:05 +01:00
pd := controller.DefaultPageData(c)
pd.Title = pd.Trans("Resend Activation Email")
2021-12-12 14:56:13 +01:00
c.HTML(http.StatusOK, "resendactivation.html", pd)
}
// ResendActivationPost handles the post request for requesting a new activation email
2021-12-12 14:56:13 +01:00
func (controller Controller) ResendActivationPost(c *gin.Context) {
2022-01-29 10:21:05 +01:00
pd := controller.DefaultPageData(c)
pd.Title = pd.Trans("Resend Activation Email")
2021-12-12 14:56:13 +01:00
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
2022-01-29 10:21:05 +01:00
go controller.sendActivationEmail(activationToken.Value, user.Email, pd.Trans)
2021-12-12 14:56:13 +01:00
} else {
// If there is no token then we need to generate a new token
2022-01-29 10:21:05 +01:00
go controller.activationEmailHandler(user.ID, user.Email, pd.Trans)
2021-12-12 14:56:13 +01:00
}
} 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",
2022-01-29 10:21:05 +01:00
Content: pd.Trans("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."),
2021-12-12 14:56:13 +01:00
})
c.HTML(http.StatusOK, "resendactivation.html", pd)
}