about summary refs log tree commit diff
path: root/src/user.go
diff options
context:
space:
mode:
authorEmile <git@emile.space>2024-11-09 16:28:47 +0100
committerEmile <git@emile.space>2024-11-09 16:28:47 +0100
commit5fb4b1f7dd6b373786a64dfe0ac59955fb6d964c (patch)
tree26ae664159632ae14b2c608d01ee4561b931bf8d /src/user.go
parentc685cc8f25adc5b2b72e4bda185fef7ec8dd6592 (diff)
yolo r2con push all
Diffstat (limited to 'src/user.go')
-rw-r--r--src/user.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/user.go b/src/user.go
index 1fd9358..cbedf03 100644
--- a/src/user.go
+++ b/src/user.go
@@ -71,6 +71,10 @@ func UserGetAll() ([]User, error) {
 	return globalState.GetAllUsers()
 }
 
+func UserGetUsernameCount(username string) (int, error) {
+	return globalState.GetUsernameCount(username)
+}
+
 //////////////////////////////////////////////////////////////////////////////
 // DATABASE
 
@@ -227,6 +231,29 @@ func (s *State) GetAllUsers() ([]User, error) {
 	return users, nil
 }
 
+func (s *State) GetUsernameCount(username string) (int, error) {
+	rows, err := s.db.Query(`
+		SELECT COUNT(*)
+		FROM users
+		WHERE name=?`, username)
+	defer rows.Close()
+	if err != nil {
+		return -1, err
+	}
+
+	var count int
+	rows.Next()
+	if err := rows.Scan(&count); err != nil {
+		return -1, err
+	}
+	if err = rows.Err(); err != nil {
+		return -1, err
+	}
+	return count, nil
+}
+
+//  return globalState.GetUsernameCount(username)
+
 //////////////////////////////////////////////////////////////////////////////
 // HTTP
 
@@ -383,6 +410,13 @@ func registerHandler(w http.ResponseWriter, r *http.Request) {
 			return
 		}
 
+		// Fetch all users and check that there isn't another user with the name here
+		if _, err := UserGetUsernameCount(username); err != nil {
+			w.WriteHeader(http.StatusInternalServerError)
+			w.Write([]byte("500 - Oi', Backend here! That username has already been taken!"))
+			return
+		}
+
 		// if we've got a password, hash it and store it and create a User
 		if password1 != "" {
 			passwordHash := argon2.IDKey([]byte(password1), []byte(os.Getenv("SALT")), 1, 64*1024, 4, 32)