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) {
|
func NewHttpHandler(params HttpHandlerParams) (HttpHandlerResult, error) {
|
||||||
verifier := emailverifier.NewVerifier()
|
|
||||||
|
|
||||||
verifier.DisableSMTPCheck()
|
|
||||||
verifier.DisableGravatarCheck()
|
|
||||||
verifier.DisableDomainSuggest()
|
|
||||||
verifier.DisableAutoUpdateDisposable()
|
|
||||||
|
|
||||||
return HttpHandlerResult{
|
return HttpHandlerResult{
|
||||||
HttpHandler: HttpHandler{
|
HttpHandler: HttpHandler{
|
||||||
verifier: verifier,
|
|
||||||
config: params.Config,
|
config: params.Config,
|
||||||
logger: params.Logger,
|
logger: params.Logger,
|
||||||
storage: params.Storage,
|
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")
|
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)
|
accountExists, _, _ := h.accounts.EmailExists(request.Email)
|
||||||
|
|
||||||
if accountExists {
|
if accountExists {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
emailverifier "github.com/AfterShip/email-verifier"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -28,5 +29,27 @@ func (u *User) BeforeUpdate(tx *gorm.DB) error {
|
||||||
if len(u.LastName) == 0 {
|
if len(u.LastName) == 0 {
|
||||||
return errors.New("last name is empty")
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getEmailVerfier() *emailverifier.Verifier {
|
||||||
|
verifier := emailverifier.NewVerifier()
|
||||||
|
|
||||||
|
verifier.DisableSMTPCheck()
|
||||||
|
verifier.DisableGravatarCheck()
|
||||||
|
verifier.DisableDomainSuggest()
|
||||||
|
verifier.DisableAutoUpdateDisposable()
|
||||||
|
|
||||||
|
return verifier
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue