Fix password reset

This commit is contained in:
Erik Michaels-Ober 2011-08-29 20:50:03 -07:00
parent 8de04a6cab
commit 4354e8e8f7
5 changed files with 42 additions and 23 deletions

View File

@ -369,8 +369,22 @@ $(function() {
$('#user_email').focus();
},
success: function() {
$('#user_forgot_password_fields').slideUp();
$('#user_sign_in_fields').slideDown();
$.ajax({
type: 'GET',
url: '/users/sign_in',
data: {
'user': {
'email': $('#user_email').val()
},
'flash': {
'notice': "Password reset instructions sent! Check your email."
}
},
success: function(data) {
activeInfoWindow.setContent(data);
$('#user_existing').click();
}
});
}
});
}

View File

@ -11,15 +11,11 @@ class PasswordsController < Devise::PasswordsController
def edit
self.resource = resource_class.new
resource.reset_password_token = params[:reset_password_token]
render_with_scope :edit
render("edit", :layout => "info_window")
end
def update
self.resource = resource_class.reset_password_by_token(params[resource_name])
if resource.errors.empty?
render(:json => {"success" => true})
else
render(:json => {"errors" => resource.errors}, :status => 500)
end
redirect_to(:controller => "main", :action => "index")
end
end

View File

@ -0,0 +1,7 @@
= form_for resource, :as => resource_name, :url => password_path(resource_name), :html => {:id => "edit_form", :method => :put} do |f|
%h2
Edit your Profile
= f.hidden_field "reset_password_token"
= f.label "password", "New password", :id => "user_password_label"
= f.password_field "password", :tabindex => 1
= f.submit "Change my password", :tabindex => 2

View File

@ -3,7 +3,7 @@
Adopt this Hydrant
#common_fields
= f.label "email", "Email address", :id => "user_email_label"
= f.text_field "email", :tabindex => 1
= f.text_field "email", :value => params[:user] ? params[:user][:email] : nil, :tabindex => 1
= f.label "new" , radio_button_tag("user", "new", true, :tabindex => 2).html_safe + "I haven't signed up yet"
= f.label "existing", radio_button_tag("user", "existing").html_safe + "I've already signed up"
#user_sign_up_fields

View File

@ -22,24 +22,26 @@ class PasswordsControllerTest < ActionController::TestCase
assert_response :error
end
# test 'should render edit view' do
# skip 'Password reset not yet implemented'
# get :edit, :reset_password_token => 'token'
# assert_response :success
# end
test 'should reset user password with an valid reset password token' do
old_password = @user.password
@user.send :generate_reset_password_token!
put :update, :user => {:reset_password_token => @user.reset_password_token, :password => 'new_password', :password_confirmation => 'new_password'}
@user.reload
assert !@user.valid_password?(old_password)
assert @user.valid_password?('new_password')
test 'should render edit view' do
get :edit, :reset_password_token => 'token'
assert_response :success
end
test 'should reset user password with an valid reset password token' do
@user.send :generate_reset_password_token!
put :update, :user => {:reset_password_token => @user.reset_password_token, :password => 'new_password', :password_confirmation => 'new_password'}
@user.reload
assert @user.valid_password?('new_password')
assert_response :redirect
assert_redirected_to :controller => 'main', :action => 'index'
end
test 'should not reset user password with an invalid reset password token' do
@user.send :generate_reset_password_token!
put :update, :user => {:reset_password_token => 'invalid_token', :password => 'new_password', :password_confirmation => 'new_password'}
assert_response :error
@user.reload
assert !@user.valid_password?('new_password')
assert_response :redirect
assert_redirected_to :controller => 'main', :action => 'index'
end
end