Sunday, 15 January 2012

ruby on rails - how to authenticate password in devise -


I want to authenticate passwords before changing the password:

  def admin_update_password admin = Admin .find (current_admin.id) @ reset = false.error_msg = hash.New #update administrator password password_old = params [: admin] [: password_old] password_new = params [: admin] [: password_new] password_new_confirmation = params [: admin ] [: Password_new_confirmation] Until password_old.blank? & Amp; & Amp; Password_new.blank? & Amp; & Amp; Password_new_confirmation.blank? If password_new == password_new_confirmation if admin.authenticate (password_old) # admin to get new passwords for front-end administrator from admin.update_attribute (: encrypted_password, BCrypt :: Password.create (password_new),) @result = true else @error_msg [: error_password] = "old password error" and er @ ender_ms g [: old_password] = "new password and old password is not the same" and endor@rar_ms [[is_nil] = "old password, new password and password Confirmation can not be blank "End Do D_ | Format | | Format.html {nothing: true} format.js {partial render: 'shared / op_result'} and end  

I googled and found that it also encrypt password encrypt to BCrypt Use gem. Therefore, I also added BCrypt gem in my project and, I add has_secure_password to my model which allows me to use if admin Authenticate (password_old) This method is valid for authentication or not.

  Class admin & lt; ActiveRecord :: Base has_secure_password termination  

After all, I got an error

 ArgumentError (wrong number of arguments (0 for 1)): 

What's more, after adding has_secure_password to the model, I can not model create . It will be fine if I type Has_secure_password

You can use it to check that a password is correct :

  Admin.valid_password? (Password_old)  

will return a boolean value.


No comments:

Post a Comment