func CreateUser(user models.User) (int, createResponse) { user.Id = uuid.NewV4().String() user.Password = Hash(user.Password) result, err := db.UserTable.Insert(user).RunWrite(db.Session) // For some ungodly strange reason rethink thinks returning errors on multiple insertions are better in the // results, than in the actual err variable. Documented here: https://github.com/rethinkdb/rethinkdb/issues/899 if err != nil || result.Errors > 0 { firstError := errors.New(result.FirstError) if r.IsConflictErr(err) || r.IsConflictErr(firstError) { return http.StatusConflict, createResponse{userAlreadyExists, nil} } panic(err) } return http.StatusCreated, createResponse{userCreated, &user} }
func changePassword(passwordChangeData changePasswordData) (int, changePasswordResponse) { if passwordChangeData.OldPassword == passwordChangeData.NewPassword { return http.StatusBadRequest, changePasswordResponse{passwordsAreTheSame} } var user models.User err := db.UserTable.Get(passwordChangeData.Email).ReadOne(&user, db.Session) if err != nil { log.Print(err) return http.StatusNotFound, changePasswordResponse{notFound} } if !ComparePasswords(user.Password, passwordChangeData.OldPassword) { return http.StatusBadRequest, changePasswordResponse{wrongPassword} } user.Password = Hash(passwordChangeData.NewPassword) _, err = db.UserTable.Get(user.Email).Update(user).RunWrite(db.Session) if err != nil { panic(err) } return http.StatusOK, changePasswordResponse{passwordChanged} }