Skip to content

Commit

Permalink
verify mail by user id (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeMyWorld authored Oct 19, 2020
1 parent 0700a06 commit b6a619e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
16 changes: 4 additions & 12 deletions datatype/request/user.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package request

import (
"regexp"
"github.com/MuShare/pluto/utils/general"
"strings"
"unicode"
)
Expand All @@ -10,8 +10,6 @@ const (
defaultDeviceID = "UnKnown Device"
)

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}$`)

type MailRegister struct {
Mail string `json:"mail"`
UserID string `json:"user_id"`
Expand All @@ -21,19 +19,12 @@ type MailRegister struct {
}

func (mr *MailRegister) Validation() bool {
if !isEmailValid(mr.Mail) || strings.TrimSpace(mr.Password) == "" || strings.TrimSpace(mr.Name) == "" {
if !general.ValidMail(mr.Mail) || strings.TrimSpace(mr.Password) == "" || strings.TrimSpace(mr.Name) == "" {
return false
}
return validateUserID(mr.UserID)
}

func isEmailValid(e string) bool {
if len(e) < 3 || len(e) > 254 {
return false
}
return emailRegex.MatchString(e)
}

type PasswordLogin struct {
Account string `json:"account" schema:"account"`
Password string `json:"password" schema:"password"`
Expand Down Expand Up @@ -130,10 +121,11 @@ func (aml *AppleMobileLogin) Validation() bool {
type RegisterVerifyMail struct {
Mail string `json:"mail"`
AppName string `json:"app_id"`
UserID string `json:"user_id"`
}

func (rvm *RegisterVerifyMail) Validation() bool {
if rvm.Mail == "" {
if rvm.Mail == "" && rvm.UserID == "" {
return false
}

Expand Down
3 changes: 2 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ Send registration verification mail
```
{
"mail": string,
"app_id": string
"app_id": string,
"user_id": string
}
```

Expand Down
43 changes: 28 additions & 15 deletions manage/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -1041,27 +1041,40 @@ func (m *Manager) RegisterWithEmail(register request.MailRegister, admin bool) (
return user, nil
}

func (m *Manager) RegisterVerifyMail(rvm request.RegisterVerifyMail) (*models.User, *perror.PlutoError) {

identifyToken := b64.RawStdEncoding.EncodeToString([]byte(rvm.Mail))
binding, err := models.Bindings(qm.Where("login_type = ? and identify_token = ?", MAILLOGIN, identifyToken)).One(m.db)
if err != nil && err == sql.ErrNoRows {
return nil, perror.MailNotExist
} else if err != nil {
return nil, perror.ServerError.Wrapper(err)
func (m *Manager) RegisterVerifyMail(rvm request.RegisterVerifyMail) (*models.Binding, *perror.PlutoError) {

var userMail string
var binding *models.Binding
var queryErr error
if rvm.Mail != "" {
userMail = rvm.Mail
identifyToken := b64.RawStdEncoding.EncodeToString([]byte(userMail))
binding, queryErr = models.Bindings(qm.Where("login_type = ? and identify_token = ?", MAILLOGIN, identifyToken)).One(m.db)
if queryErr != nil && queryErr == sql.ErrNoRows {
return nil, perror.MailNotExist
} else if queryErr != nil {
return nil, perror.ServerError.Wrapper(queryErr)
}
} else {
user, userErr := models.Users(qm.Where("user_id = ?", rvm.UserID)).One(m.db)
if userErr != nil && userErr == sql.ErrNoRows {
return nil, perror.UserIdNotExist
} else if userErr != nil {
return nil, perror.ServerError.Wrapper(userErr)
}
binding, queryErr = models.Bindings(qm.Where("login_type = ? and user_id = ?", MAILLOGIN, user.ID)).One(m.db)
if queryErr != nil && queryErr == sql.ErrNoRows {
return nil, perror.MailNotExist
} else if queryErr != nil {
return nil, perror.ServerError.Wrapper(queryErr)
}
}

if binding.Verified.Bool == true {
return nil, perror.MailAlreadyVerified
}

user, err := models.Users(qm.Where("id = ?", binding.UserID)).One(m.db)

if err != nil {
return nil, perror.ServerError.Wrapper(err)
}

return user, nil
return binding, nil
}

func (m *Manager) RegisterVerify(token string) *perror.PlutoError {
Expand Down
4 changes: 2 additions & 2 deletions route/v1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (router *Router) VerifyMail(w http.ResponseWriter, r *http.Request) *perror
return perr
}

user, perr := router.manager.RegisterVerifyMail(rvm)
binding, perr := router.manager.RegisterVerifyMail(rvm)

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

0 comments on commit b6a619e

Please sign in to comment.