From 080bef354d6974c4198e98f6799b839fe3def4ff Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 19 Mar 2024 09:44:44 -0400 Subject: [PATCH] refactor: check if the email is the same and return a new error for it --- account/account.go | 6 +++++- account/errors.go | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/account/account.go b/account/account.go index e1e6605..cb8c7a0 100644 --- a/account/account.go +++ b/account/account.go @@ -284,7 +284,7 @@ func (s AccountServiceDefault) UpdateAccountEmail(userId uint, email string, pas return NewAccountError(ErrKeyEmailAlreadyExists, nil) } - valid, _, err := s.ValidLoginByUserID(userId, password) + valid, user, err := s.ValidLoginByUserID(userId, password) if err != nil { return err } @@ -293,6 +293,10 @@ func (s AccountServiceDefault) UpdateAccountEmail(userId uint, email string, pas return NewAccountError(ErrKeyInvalidLogin, nil) } + if user.Email == email { + return NewAccountError(ErrKeyUpdatingSameEmail, nil) + } + var update models.User update.Email = email diff --git a/account/errors.go b/account/errors.go index ebc4b10..9e41192 100644 --- a/account/errors.go +++ b/account/errors.go @@ -9,6 +9,7 @@ const ( // Account creation errors ErrKeyAccountCreationFailed = "ErrAccountCreationFailed" ErrKeyEmailAlreadyExists = "ErrEmailAlreadyExists" + ErrKeyUpdatingSameEmail = "ErrUpdatingSameEmail" ErrKeyPasswordHashingFailed = "ErrPasswordHashingFailed" // Account lookup and existence verification errors @@ -56,6 +57,7 @@ var defaultErrorMessages = map[string]string{ ErrKeyAccountCreationFailed: "Account creation failed due to an internal error.", ErrKeyEmailAlreadyExists: "The email address provided is already in use.", 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 ErrKeyUserNotFound: "The requested user was not found.",