Exemple #1
0
// EmailVerification verifies an email of user.
func EmailVerification(c *gin.Context) (int, error) {
	var user model.User
	var verifyEmailForm VerifyEmailForm
	c.BindWith(&verifyEmailForm, binding.Form)
	log.Debugf("verifyEmailForm.ActivationToken : %s", verifyEmailForm.ActivationToken)
	if db.ORM.Where(&model.User{ActivationToken: verifyEmailForm.ActivationToken}).First(&user).RecordNotFound() {
		return http.StatusNotFound, errors.New("User is not found.")
	}
	isExpired := timeHelper.IsExpired(user.ActivateUntil)
	log.Debugf("passwordResetUntil : %s", user.ActivateUntil.UTC())
	log.Debugf("expired : %t", isExpired)
	if isExpired {
		return http.StatusForbidden, errors.New("token not valid.")
	}
	user.ActivationToken = ""
	user.ActivateUntil = time.Now()
	user.ActivatedAt = time.Now()
	user.Activation = true
	status, err := UpdateUserCore(&user)
	if err != nil {
		return status, err
	}
	status, err = SetCookie(c, user.Token)
	return status, err
}
Exemple #2
0
// SendVerificationToUser sends an email verification token to user.
func SendVerificationToUser(user model.User) (int, error) {
	var status int
	var err error
	user.ActivateUntil = timeHelper.TwentyFourHoursLater()
	user.ActivationToken, err = crypto.GenerateRandomToken32()
	if err != nil {
		return http.StatusInternalServerError, err
	}
	user.Activation = false
	log.Debugf("generated token : %s", user.ActivationToken)
	status, err = UpdateUserCore(&user)
	if err != nil {
		return status, err
	}
	err = SendEmailVerfication(user.Email, user.ActivationToken, "en-us")
	if err != nil {
		return http.StatusInternalServerError, err
	}
	return http.StatusOK, err
}