register return error instead of bool

This commit is contained in:
Your Name 2024-03-01 09:37:41 -06:00
parent 972110aded
commit 0cb02fb65f
3 changed files with 10 additions and 10 deletions

View File

@ -40,8 +40,8 @@ func main() {
scsusers.Init(db, "test", "Example Test", "nobody@nowhere.com", "localhost:25", true) scsusers.Init(db, "test", "Example Test", "nobody@nowhere.com", "localhost:25", true)
a:=scsusers.UsernameAvailable("testuser") a:=scsusers.UsernameAvailable("testuser")
fmt.Printf("Initial test of username available: %v\n", a) fmt.Printf("Initial test of username available: %v\n", a)
a=scsusers.Register("testuser", email, "127.0.0.1") err=scsusers.Register("testuser", email, "127.0.0.1")
fmt.Printf("Register returned %v\n",a) fmt.Printf("Register returned %v\n",err)
fmt.Printf("Attempt to log in with invalid username returned %v\n", scsusers.Login("baduser", "badpass")) fmt.Printf("Attempt to log in with invalid username returned %v\n", scsusers.Login("baduser", "badpass"))
fmt.Printf("Enter code from email:") fmt.Printf("Enter code from email:")
var code string var code string

12
main.go
View File

@ -79,9 +79,9 @@ func UsernameAvailable(username string) bool {
/* Check for username availability, add to database, send email */ /* Check for username availability, add to database, send email */
func Register(username, email, ip string) bool { func Register(username, email, ip string) error {
if !UsernameAvailable(username) { if !UsernameAvailable(username) {
return false return fmt.Errorf("Username %s is not available", username)
} }
username = strings.ToLower(username) username = strings.ToLower(username)
@ -89,17 +89,17 @@ func Register(username, email, ip string) bool {
crypt, err := bcrypt.GenerateFromPassword(pass, 10) crypt, err := bcrypt.GenerateFromPassword(pass, 10)
if err != nil { if err != nil {
log.Printf("scsusers.Register: Bcrypt GenerateFromPassword failed? Pass is %s and error is %s\n", pass, err.Error()) log.Printf("scsusers.Register: Bcrypt GenerateFromPassword failed? Pass is %s and error is %s\n", pass, err.Error())
return false return fmt.Errorf("Failed to generate password")
} }
_, err = c.db.Query(fmt.Sprintf("insert into %s_users (username, password) VALUES (?,?)", c.TablePrefix), username, crypt) _, err = c.db.Query(fmt.Sprintf("insert into %s_users (username, password) VALUES (?,?)", c.TablePrefix), username, crypt)
if err != nil { if err != nil {
log.Printf("scsusers.Register: insert failed: %s\n", err.Error()) log.Printf("scsusers.Register: insert failed: %s\n", err.Error())
return false return fmt.Errorf("Failed to insert user")
} }
if SendRegistrationEmail(email, username, string(pass)) { if SendRegistrationEmail(email, username, string(pass)) {
log.Printf("scsusers.Register: New user registration: %s from %s\n", username, ip) log.Printf("scsusers.Register: New user registration: %s from %s\n", username, ip)
return true return nil
} }
log.Printf("scsusers.Register: Failed to send registration email, deleting user %s\n", username) log.Printf("scsusers.Register: Failed to send registration email, deleting user %s\n", username)
q := fmt.Sprintf("delete from %s_users where username = ? AND password=?", c.TablePrefix) q := fmt.Sprintf("delete from %s_users where username = ? AND password=?", c.TablePrefix)
@ -107,7 +107,7 @@ func Register(username, email, ip string) bool {
if err != nil { if err != nil {
log.Printf("scsusers.Register: Failed to delete new user %s: %s\n", username, err.Error()) log.Printf("scsusers.Register: Failed to delete new user %s: %s\n", username, err.Error())
} }
return false return fmt.Errorf("Failed to send registration email")
} }
func NewUser() *UserData { func NewUser() *UserData {

View File

@ -40,8 +40,8 @@ func TestUsers(t *testing.T) {
Init(db, "test", "Example Test", "nobody@nowhere.com", "localhost:25", true) Init(db, "test", "Example Test", "nobody@nowhere.com", "localhost:25", true)
a := UsernameAvailable("testuser") a := UsernameAvailable("testuser")
fmt.Printf("Initial test of username available: %v\n", a) fmt.Printf("Initial test of username available: %v\n", a)
a = Register("testuser", email, "127.0.0.1") err = Register("testuser", email, "127.0.0.1")
fmt.Printf("Register returned %v\n", a) fmt.Printf("Register returned %v\n", err)
fmt.Printf("Attempt to log in with invalid username returned %v\n", Login("baduser", "badpass")) fmt.Printf("Attempt to log in with invalid username returned %v\n", Login("baduser", "badpass"))
fmt.Printf("Enter code from email:") fmt.Printf("Enter code from email:")
var code string var code string