refactor: move email validation to gorm

This commit is contained in:
Derrick Hammer 2024-02-13 19:41:00 -05:00
parent 302821d749
commit 6f61f09ba4
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 23 additions and 15 deletions

View File

@ -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 {

View File

@ -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
}