Restful Authentication - Change password

When using the Restful Authentication plugin and you would like to allow users to change their password, consider using this code as a base

In your users controller

    #
    # Change user passowrd
    def change_password

    end
    
    
    #
    # Change user passowrd
    def change_password_update
        if User.authenticate(current_user.login, params[:old_password])
            if ((params[:password] == params[:password_confirmation]) && !params[:password_confirmation].blank?)
                current_user.password_confirmation = params[:password_confirmation]
                current_user.password = params[:password]
                
                if current_user.save!
                    flash[:notice] = "Password successfully updated"
                    redirect_to change_password_path
                else
                    flash[:alert] = "Password not changed"
                    render :action => 'change_password'
                end
                 
            else
                flash[:alert] = "New Password mismatch" 
                render :action => 'change_password'
            end
        else
            flash[:alert] = "Old password incorrect" 
            render :action => 'change_password'
        end
    end



Create a view users/change_password.html.erb

<% form_tag change_password_update_user_path(current_user), :method => :put do |f| %>
    
                   
            Old Password
            <%= password_field_tag 'old_password', @old_password, :size => 45, :class => 'text' %>
            
            New Password
            <%= password_field_tag 'password', {}, :size => 45, :class => 'text' %>
            Between 4 and 40 characters
            
            Confirm new password
            <%= password_field_tag 'password_confirmation', {}, :size => 45, :class => 'text' %>

        
        <%= submit_tag 'Change password' %>
    
<%end%>



Finall add this to your config/routes.rb file

map.change_password '/change_password', :controller => 'users', :action => 'change_password'

Edit documentation | Back in time (2 older versions) | Last edited by: Guest, about 1 month ago