Added sending an email to confirm new users email addresses

This commit is contained in:
kolaente 2018-10-27 12:23:41 +02:00
parent 0cfea682ea
commit 902ed1f958
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 40 additions and 0 deletions

View File

@ -18,8 +18,10 @@ type User struct {
Username string `xorm:"varchar(250) not null unique" json:"username"`
Password string `xorm:"varchar(250) not null" json:"-"`
Email string `xorm:"varchar(250)" json:"email"`
IsActive bool `json:"-"`
PasswordResetToken string `xorm:"varchar(450)" json:"-"`
EmailConfirmToken string `xorm:"varchar(450)" json:"-"`
Created int64 `xorm:"created" json:"-"`
Updated int64 `xorm:"updated" json:"-"`

View File

@ -1,6 +1,8 @@
package models
import (
"code.vikunja.io/api/models/mail"
"code.vikunja.io/api/models/utils"
"golang.org/x/crypto/bcrypt"
)
@ -48,6 +50,12 @@ func CreateUser(user User) (newUser User, err error) {
return User{}, err
}
// Generate a confirm token
newUser.EmailConfirmToken = utils.MakeRandomString(150)
// The new user should not be activated until it confirms his mail address
newUser.IsActive = false
// Insert it
_, err = x.Insert(newUser)
if err != nil {
@ -67,6 +75,13 @@ func CreateUser(user User) (newUser User, err error) {
return User{}, err
}
// Send the user a mail with a link to confirm the mail
data := map[string]interface{}{
"User": newUserOut,
}
mail.SendMailWithTemplate(user.Email, newUserOut.Username+" + Vikunja = <3", "confirm-email", data)
return newUserOut, err
}

View File

@ -0,0 +1,16 @@
{{template "mail-header.tmpl" .}}
<p>
Hi {{.User.Username}},<br>
<br>
Welcome to Vikunja!
<br/>
To confirm you email address, click the link below:
</p>
<a href="{{.FrontendURL}}?userEmailConfirm={{.User.EmailConfirmToken}}" title="Confirm your email address" style="background: rgb(20, 131, 175); -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; border: 1px solid rgb(16, 106, 140); border-bottom-width: 3px; color: rgb(255, 255, 255); font-weight: 700; font-size: 13px; margin: 10px auto; padding: 5px 10px; text-decoration: none; text-align: center; text-rendering: optimizelegibility; text-transform: uppercase; display: block; width: 200px;">
Confirm your email address
</a>
<p>
If the button above doesn't work, copy the url below and paste it in your browsers address bar:<br/>
{{.FrontendURL}}?userEmailConfirm={{.User.EmailConfirmToken}}
</p>
{{template "mail-footer.tmpl"}}

View File

@ -0,0 +1,7 @@
Hi {{.User.Username}},
Welcome to Vikunja!
To confirm you email address, click the link below:
{{.User.EmailConfirmToken}}