small pixel drawing of a pufferfish vore

add better error handling for user creation
Jes Olson j3s@c3f.net
Mon, 20 Mar 2023 19:17:13 -0700
commit

56d74549d2b53f4b941eb5521ecef6090bc6d349

parent

abc4ef8b842d0e8be9a2957c01eb4abdad3a23a6

2 files changed, 9 insertions(+), 5 deletions(-)

jump to
M site.gosite.go

@@ -243,12 +243,18 @@ return nil

} func (s *Site) register(username string, password string) error { + if s.db.UserExists(username) { + return fmt.Errorf("user '%s' already exists", username) + } hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil { return err } - s.db.AddUser(username, string(hashedPassword)) + err = s.db.AddUser(username, string(hashedPassword)) + if err != nil { + return err + } return nil }
M sqlite/sql.gosqlite/sql.go

@@ -87,11 +87,9 @@ panic(err)

} } -func (s *DB) AddUser(username string, passwordHash string) { +func (s *DB) AddUser(username string, passwordHash string) error { _, err := s.sql.Exec("INSERT INTO user (username, password) VALUES (?, ?)", username, passwordHash) - if err != nil { - panic(err) - } + return err } func (s *DB) Subscribe(username string, feedURL string) {