add better error handling for user creation
Jes Olson j3s@c3f.net
Mon, 20 Mar 2023 19:17:13 -0700
2 files changed,
9 insertions(+),
5 deletions(-)
M
site.go
→
site.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.go
→
sqlite/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) {