Skip to content

Commit b6a619e

Browse files
authored
verify mail by user id (#21)
1 parent 0700a06 commit b6a619e

4 files changed

Lines changed: 36 additions & 30 deletions

File tree

datatype/request/user.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package request
22

33
import (
4-
"regexp"
4+
"github.com/MuShare/pluto/utils/general"
55
"strings"
66
"unicode"
77
)
@@ -10,8 +10,6 @@ const (
1010
defaultDeviceID = "UnKnown Device"
1111
)
1212

13-
var emailRegex = regexp.MustCompile(`^[0-9a-z][_.0-9a-z-]{0,31}@([0-9a-z][0-9a-z-]{0,30}[0-9a-z]\.){1,4}[a-z]{2,4}$`)
14-
1513
type MailRegister struct {
1614
Mail string `json:"mail"`
1715
UserID string `json:"user_id"`
@@ -21,19 +19,12 @@ type MailRegister struct {
2119
}
2220

2321
func (mr *MailRegister) Validation() bool {
24-
if !isEmailValid(mr.Mail) || strings.TrimSpace(mr.Password) == "" || strings.TrimSpace(mr.Name) == "" {
22+
if !general.ValidMail(mr.Mail) || strings.TrimSpace(mr.Password) == "" || strings.TrimSpace(mr.Name) == "" {
2523
return false
2624
}
2725
return validateUserID(mr.UserID)
2826
}
2927

30-
func isEmailValid(e string) bool {
31-
if len(e) < 3 || len(e) > 254 {
32-
return false
33-
}
34-
return emailRegex.MatchString(e)
35-
}
36-
3728
type PasswordLogin struct {
3829
Account string `json:"account" schema:"account"`
3930
Password string `json:"password" schema:"password"`
@@ -130,10 +121,11 @@ func (aml *AppleMobileLogin) Validation() bool {
130121
type RegisterVerifyMail struct {
131122
Mail string `json:"mail"`
132123
AppName string `json:"app_id"`
124+
UserID string `json:"user_id"`
133125
}
134126

135127
func (rvm *RegisterVerifyMail) Validation() bool {
136-
if rvm.Mail == "" {
128+
if rvm.Mail == "" && rvm.UserID == "" {
137129
return false
138130
}
139131

docs/api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ Send registration verification mail
7171
```
7272
{
7373
"mail": string,
74-
"app_id": string
74+
"app_id": string,
75+
"user_id": string
7576
}
7677
```
7778

manage/user.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,27 +1041,40 @@ func (m *Manager) RegisterWithEmail(register request.MailRegister, admin bool) (
10411041
return user, nil
10421042
}
10431043

1044-
func (m *Manager) RegisterVerifyMail(rvm request.RegisterVerifyMail) (*models.User, *perror.PlutoError) {
1045-
1046-
identifyToken := b64.RawStdEncoding.EncodeToString([]byte(rvm.Mail))
1047-
binding, err := models.Bindings(qm.Where("login_type = ? and identify_token = ?", MAILLOGIN, identifyToken)).One(m.db)
1048-
if err != nil && err == sql.ErrNoRows {
1049-
return nil, perror.MailNotExist
1050-
} else if err != nil {
1051-
return nil, perror.ServerError.Wrapper(err)
1044+
func (m *Manager) RegisterVerifyMail(rvm request.RegisterVerifyMail) (*models.Binding, *perror.PlutoError) {
1045+
1046+
var userMail string
1047+
var binding *models.Binding
1048+
var queryErr error
1049+
if rvm.Mail != "" {
1050+
userMail = rvm.Mail
1051+
identifyToken := b64.RawStdEncoding.EncodeToString([]byte(userMail))
1052+
binding, queryErr = models.Bindings(qm.Where("login_type = ? and identify_token = ?", MAILLOGIN, identifyToken)).One(m.db)
1053+
if queryErr != nil && queryErr == sql.ErrNoRows {
1054+
return nil, perror.MailNotExist
1055+
} else if queryErr != nil {
1056+
return nil, perror.ServerError.Wrapper(queryErr)
1057+
}
1058+
} else {
1059+
user, userErr := models.Users(qm.Where("user_id = ?", rvm.UserID)).One(m.db)
1060+
if userErr != nil && userErr == sql.ErrNoRows {
1061+
return nil, perror.UserIdNotExist
1062+
} else if userErr != nil {
1063+
return nil, perror.ServerError.Wrapper(userErr)
1064+
}
1065+
binding, queryErr = models.Bindings(qm.Where("login_type = ? and user_id = ?", MAILLOGIN, user.ID)).One(m.db)
1066+
if queryErr != nil && queryErr == sql.ErrNoRows {
1067+
return nil, perror.MailNotExist
1068+
} else if queryErr != nil {
1069+
return nil, perror.ServerError.Wrapper(queryErr)
1070+
}
10521071
}
10531072

10541073
if binding.Verified.Bool == true {
10551074
return nil, perror.MailAlreadyVerified
10561075
}
10571076

1058-
user, err := models.Users(qm.Where("id = ?", binding.UserID)).One(m.db)
1059-
1060-
if err != nil {
1061-
return nil, perror.ServerError.Wrapper(err)
1062-
}
1063-
1064-
return user, nil
1077+
return binding, nil
10651078
}
10661079

10671080
func (m *Manager) RegisterVerify(token string) *perror.PlutoError {

route/v1/user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func (router *Router) VerifyMail(w http.ResponseWriter, r *http.Request) *perror
296296
return perr
297297
}
298298

299-
user, perr := router.manager.RegisterVerifyMail(rvm)
299+
binding, perr := router.manager.RegisterVerifyMail(rvm)
300300

301301
if perr != nil {
302302
return perr
@@ -309,7 +309,7 @@ func (router *Router) VerifyMail(w http.ResponseWriter, r *http.Request) *perror
309309
}
310310
language := r.Header.Get("Accept-Language")
311311
appI18nName, err := router.manager.ApplicationI18nName(rvm.AppName, language)
312-
if err := ml.SendRegisterVerify(user.ID, rvm.Mail, routeUtils.GetBaseURL(r), language, appI18nName); err != nil {
312+
if err := ml.SendRegisterVerify(binding.UserID, binding.Mail, routeUtils.GetBaseURL(r), language, appI18nName); err != nil {
313313
router.logger.Error(err.LogError.Error())
314314
}
315315
}()

0 commit comments

Comments
 (0)