UpdateUserPassword updates a user's password with validation
(db *gorm.DB, user *database.User, newPassword string)
| 144 | |
| 145 | // UpdateUserPassword updates a user's password with validation |
| 146 | func UpdateUserPassword(db *gorm.DB, user *database.User, newPassword string) error { |
| 147 | // Validate password |
| 148 | if err := validatePassword(newPassword); err != nil { |
| 149 | return err |
| 150 | } |
| 151 | |
| 152 | // Hash the password |
| 153 | hashedPassword, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost) |
| 154 | if err != nil { |
| 155 | return pkgErrors.Wrap(err, "hashing password") |
| 156 | } |
| 157 | |
| 158 | // Update the password |
| 159 | if err := db.Model(&user).Update("password", string(hashedPassword)).Error; err != nil { |
| 160 | return pkgErrors.Wrap(err, "updating password") |
| 161 | } |
| 162 | |
| 163 | return nil |
| 164 | } |
| 165 | |
| 166 | // RemoveUser removes a user from the system |
| 167 | // Returns an error if the user has any notes or books |