package api import ( "lishwist/http/templates" ) type RegisterProps struct { GeneralError string `json:",omitempty"` Username templates.InputProps Password templates.InputProps ConfirmPassword templates.InputProps } func (p *RegisterProps) Validate() (valid bool) { valid = true if p.Password.Value != p.ConfirmPassword.Value { p.ConfirmPassword.Error = "Passwords didn't match" valid = false } if !p.Username.Validate() { valid = false } if !p.Password.Validate() { valid = false } if !p.ConfirmPassword.Validate() { valid = false } return } func NewRegisterProps(usernameVal, passwordVal, confirmPassVal string) *RegisterProps { return &RegisterProps{ GeneralError: "", Username: templates.InputProps{ Name: "username", Required: true, MinLength: 4, Value: usernameVal, }, Password: templates.InputProps{ Type: "password", Name: "newPassword", Required: true, MinLength: 5, Value: passwordVal, }, ConfirmPassword: templates.InputProps{ Type: "password", Name: "confirmPassword", Required: true, Value: confirmPassVal, }, } } // func Register(username, newPassword, confirmPassword string) *RegisterProps { // props := NewRegisterProps(username, newPassword, confirmPassword) // valid := props.Validate() // props.Password.Value = "" // props.ConfirmPassword.Value = "" // if !valid { // log.Printf("Invalid props: %#v\n", props) // return props // } // existingUser, _ := db.GetUserByName(username) // if existingUser != nil { // log.Printf("Username is taken: %q\n", existingUser.NormalName) // props.Username.Error = "Username is taken" // return props // } // hashedPasswordBytes, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.MinCost) // if err != nil { // log.Printf("Failed to hash password: %s\n", err) // props.GeneralError = "Something went wrong. Error code: Aang" // return props // } // _, err = db.CreateUser(username, hashedPasswordBytes) // if err != nil { // log.Printf("Failed to create user: %s\n", err) // props.GeneralError = "Something went wrong. Error code: Ozai" // return props // } // return nil // }