refactor: move email validation to gorm
This commit is contained in:
parent
302821d749
commit
6f61f09ba4
|
@ -106,16 +106,8 @@ type HttpHandlerResult struct {
|
|||
}
|
||||
|
||||
func NewHttpHandler(params HttpHandlerParams) (HttpHandlerResult, error) {
|
||||
verifier := emailverifier.NewVerifier()
|
||||
|
||||
verifier.DisableSMTPCheck()
|
||||
verifier.DisableGravatarCheck()
|
||||
verifier.DisableDomainSuggest()
|
||||
verifier.DisableAutoUpdateDisposable()
|
||||
|
||||
return HttpHandlerResult{
|
||||
HttpHandler: HttpHandler{
|
||||
verifier: verifier,
|
||||
config: params.Config,
|
||||
logger: params.Logger,
|
||||
storage: params.Storage,
|
||||
|
@ -398,13 +390,6 @@ func (h *HttpHandler) accountRegister(jc jape.Context) {
|
|||
request.Email = fmt.Sprintf("%s@%s", hex.EncodeToString(decodedKey[1:]), "example.com")
|
||||
}
|
||||
|
||||
verify, _ := h.verifier.Verify(request.Email)
|
||||
|
||||
if !verify.Syntax.Valid {
|
||||
errored(errInvalidEmail)
|
||||
return
|
||||
}
|
||||
|
||||
accountExists, _, _ := h.accounts.EmailExists(request.Email)
|
||||
|
||||
if accountExists {
|
||||
|
|
|
@ -2,6 +2,7 @@ package models
|
|||
|
||||
import (
|
||||
"errors"
|
||||
emailverifier "github.com/AfterShip/email-verifier"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
|
@ -28,5 +29,27 @@ func (u *User) BeforeUpdate(tx *gorm.DB) error {
|
|||
if len(u.LastName) == 0 {
|
||||
return errors.New("last name is empty")
|
||||
}
|
||||
|
||||
verify, err := getEmailVerfier().Verify(u.Email)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !verify.Syntax.Valid {
|
||||
return errors.New("email is invalid")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getEmailVerfier() *emailverifier.Verifier {
|
||||
verifier := emailverifier.NewVerifier()
|
||||
|
||||
verifier.DisableSMTPCheck()
|
||||
verifier.DisableGravatarCheck()
|
||||
verifier.DisableDomainSuggest()
|
||||
verifier.DisableAutoUpdateDisposable()
|
||||
|
||||
return verifier
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue