Added check if the emailaddress is already used when creating a new user
This commit is contained in:
parent
63a74ee7ac
commit
f09e5c6f84
|
@ -22,6 +22,22 @@ func (err ErrUsernameExists) Error() string {
|
|||
return fmt.Sprintf("a user with this username does already exist [user id: %d, username: %s]", err.UserID, err.Username)
|
||||
}
|
||||
|
||||
// ErrUserEmailExists represents a "UserEmailExists" kind of error.
|
||||
type ErrUserEmailExists struct {
|
||||
UserID int64
|
||||
Email string
|
||||
}
|
||||
|
||||
// IsErrUserEmailExists checks if an error is a ErrUserEmailExists.
|
||||
func IsErrUserEmailExists(err error) bool {
|
||||
_, ok := err.(ErrUserEmailExists)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrUserEmailExists) Error() string {
|
||||
return fmt.Sprintf("a user with this email does already exist [user id: %d, email: %s]", err.UserID, err.Email)
|
||||
}
|
||||
|
||||
// ErrNoUsername represents a "UsernameAlreadyExists" kind of error.
|
||||
type ErrNoUsername struct {
|
||||
UserID int64
|
||||
|
|
|
@ -14,7 +14,7 @@ func CreateUser(user User) (newUser User, err error) {
|
|||
return User{}, ErrNoUsernamePassword{}
|
||||
}
|
||||
|
||||
// Check if the user already existst
|
||||
// Check if the user already existst with that username
|
||||
existingUser, exists, err := GetUser(User{Username: newUser.Username})
|
||||
if err != nil {
|
||||
return User{}, err
|
||||
|
@ -23,6 +23,15 @@ func CreateUser(user User) (newUser User, err error) {
|
|||
return User{}, ErrUsernameExists{existingUser.ID, existingUser.Username}
|
||||
}
|
||||
|
||||
// Check if the user already existst with that username
|
||||
existingUser, exists, err = GetUser(User{Email:newUser.Email})
|
||||
if err != nil {
|
||||
return User{}, err
|
||||
}
|
||||
if exists {
|
||||
return User{}, ErrUserEmailExists{existingUser.ID, existingUser.Email}
|
||||
}
|
||||
|
||||
// Hash the password
|
||||
newUser.Password, err = hashPassword(user.Password)
|
||||
if err != nil {
|
||||
|
|
|
@ -37,8 +37,14 @@ func TestCreateUser(t *testing.T) {
|
|||
assert.Error(t, err)
|
||||
|
||||
// Check if it fails to create a user with just the same username
|
||||
_, err = CreateUser(User{Username:dummyuser.Username})
|
||||
_, err = CreateUser(User{Username:dummyuser.Username, Password: "1234"})
|
||||
assert.Error(t, err)
|
||||
assert.True(t, IsErrUsernameExists(err))
|
||||
|
||||
// Check if it fails to create one with the same email
|
||||
_, err = CreateUser(User{Username: "noone", Password: "1234", Email:dummyuser.Email})
|
||||
assert.Error(t, err)
|
||||
assert.True(t, IsErrUserEmailExists(err))
|
||||
|
||||
// Check if it fails to create a user without password and username
|
||||
_, err = CreateUser(User{})
|
||||
|
|
|
@ -68,6 +68,11 @@ func UserAddOrUpdate(c echo.Context) error {
|
|||
return c.JSON(http.StatusBadRequest, models.Message{"A user with this username already exists."})
|
||||
}
|
||||
|
||||
// Check for user with that email already exists
|
||||
if models.IsErrUserEmailExists(err) {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"A user with this email address already exists."})
|
||||
}
|
||||
|
||||
// Check for no username provided
|
||||
if models.IsErrNoUsername(err) {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"Please specify a username."})
|
||||
|
|
Loading…
Reference in New Issue