Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check current password in Rails (Devise gem)?

I'm failing to check current password using jQuery validation. I'm using Devise gem and jQuery validation. Firebug says:

the problem is in my controller :

def checkpass
 user = User.find_by_email(params[:user][:email])
 respond_to do |format|
 if user && user.authenticate(params[:user][:password])
    format.json { render :json => @user }
 end

end

with this code Firebug gives me error

 406 Not Acceptable

end

example of checkemail uniqueness action:

 def checkemail
  @user = User.find_by_email(params[:user][:email])
   respond_to do |format|
   format.json { render :json => !@user }
  end
end

My js:

$("#edit_password").validate({
   "user[current_password]":{
            required: true,
            minlength: 6,
            remote: {
          url: "http://127.0.0.1:3000/checkpass"
          }
 ...
 })

and HTML:

   <input type="password" size="30" name="user[current_password]" id="user_current_password" class="valid">

Can someone suggest how to do it ?

like image 873
MID Avatar asked Dec 08 '22 20:12

MID


2 Answers

I am using rails version 4 and when I tried doing user.authenticate I got error saying unknown methods, so I found the other method for checking if password is valid,

valid_password?(current_password)

I thought to share it so it might help someone else as well.

like image 99
talal7860 Avatar answered Dec 29 '22 18:12

talal7860


in the controller in function check_pass

  user = User.find_by_email(params[:user][:email])
  respond_to do |format|
     if user && user.authenticate(params[:user][:password])
        format.json { render :json => @user }
     end
  end
like image 21
Aayush Khandelwal Avatar answered Dec 29 '22 18:12

Aayush Khandelwal