From 5fb4b1f7dd6b373786a64dfe0ac59955fb6d964c Mon Sep 17 00:00:00 2001 From: Emile Date: Sat, 9 Nov 2024 16:28:47 +0100 Subject: yolo r2con push all --- src/user.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src/user.go') 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) -- cgit 1.4.1