refactor: check if the email is the same and return a new error for it

This commit is contained in:
Derrick Hammer 2024-03-19 09:44:44 -04:00
parent 1d60cbf532
commit 080bef354d
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 7 additions and 1 deletions

View File

@ -284,7 +284,7 @@ func (s AccountServiceDefault) UpdateAccountEmail(userId uint, email string, pas
return NewAccountError(ErrKeyEmailAlreadyExists, nil) return NewAccountError(ErrKeyEmailAlreadyExists, nil)
} }
valid, _, err := s.ValidLoginByUserID(userId, password) valid, user, err := s.ValidLoginByUserID(userId, password)
if err != nil { if err != nil {
return err return err
} }
@ -293,6 +293,10 @@ func (s AccountServiceDefault) UpdateAccountEmail(userId uint, email string, pas
return NewAccountError(ErrKeyInvalidLogin, nil) return NewAccountError(ErrKeyInvalidLogin, nil)
} }
if user.Email == email {
return NewAccountError(ErrKeyUpdatingSameEmail, nil)
}
var update models.User var update models.User
update.Email = email update.Email = email

View File

@ -9,6 +9,7 @@ const (
// Account creation errors // Account creation errors
ErrKeyAccountCreationFailed = "ErrAccountCreationFailed" ErrKeyAccountCreationFailed = "ErrAccountCreationFailed"
ErrKeyEmailAlreadyExists = "ErrEmailAlreadyExists" ErrKeyEmailAlreadyExists = "ErrEmailAlreadyExists"
ErrKeyUpdatingSameEmail = "ErrUpdatingSameEmail"
ErrKeyPasswordHashingFailed = "ErrPasswordHashingFailed" ErrKeyPasswordHashingFailed = "ErrPasswordHashingFailed"
// Account lookup and existence verification errors // Account lookup and existence verification errors
@ -56,6 +57,7 @@ var defaultErrorMessages = map[string]string{
ErrKeyAccountCreationFailed: "Account creation failed due to an internal error.", ErrKeyAccountCreationFailed: "Account creation failed due to an internal error.",
ErrKeyEmailAlreadyExists: "The email address provided is already in use.", ErrKeyEmailAlreadyExists: "The email address provided is already in use.",
ErrKeyPasswordHashingFailed: "Failed to secure the password, please try again later.", ErrKeyPasswordHashingFailed: "Failed to secure the password, please try again later.",
ErrKeyUpdatingSameEmail: "The email address provided is the same as your current one.",
// Account lookup and existence verification errors // Account lookup and existence verification errors
ErrKeyUserNotFound: "The requested user was not found.", ErrKeyUserNotFound: "The requested user was not found.",