From d7810ddd2b0c95f17e38712a96c02c0a1f8d9a37 Mon Sep 17 00:00:00 2001 From: Shuhei Kitagawa Date: Sun, 14 Jan 2018 07:35:58 +0900 Subject: [PATCH] Fix to raise error when trying to register a duplicate username (#926) This corrects a minor error from recent changes in which registering an existing username didn't throw an error. --- auth/auth.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/auth/auth.go b/auth/auth.go index 3deb327c..e371b3a4 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -41,6 +41,9 @@ var ErrEmptyPassword = errors.New("Password cannot be blank") // ErrPasswordMismatch is thrown when a user provides passwords that do not match var ErrPasswordMismatch = errors.New("Passwords must match") +// ErrUsernameTaken is thrown when a user attempts to register a username that is taken. +var ErrUsernameTaken = errors.New("Username already taken") + // Login attempts to login the user given a request. func Login(r *http.Request) (bool, models.User, error) { username, password := r.FormValue("username"), r.FormValue("password") @@ -63,11 +66,17 @@ func Register(r *http.Request) (bool, error) { newPassword := r.FormValue("password") confirmPassword := r.FormValue("confirm_password") u, err := models.GetUserByUsername(username) + // If the given username already exists, throw an error and return false + if err == nil { + return false, ErrUsernameTaken + } + // If we have an error which is not simply indicating that no user was found, report it if err != nil && err != gorm.ErrRecordNotFound { fmt.Println(err) return false, err } + u = models.User{} // If we've made it here, we should have a valid username given // Check that the passsword isn't blank