Search code examples
ruby-on-railsauthlogic

Test if this password is correct for current logged in user


I'm building a "change password" form for my user built with these fields:

  • Old password
  • New password
  • Confirmation password

I need a way to check if the current logged in user password is the same as "old password" field, are there any possibility to do this, with authlogic? I can't find a method to test a password.


Solution

  • Authlogic has a valid_password? method. see: http://rubydoc.info/github/binarylogic/authlogic/master/Authlogic/ActsAsAuthentic/Password/Methods/InstanceMethods#valid_password%3F-instance_method

    So you could

    if @user.valid_password?(params[:old_password])
      @user.password = params[:new_password]
      @user.password_confirmation = params[:new_password_confirmation]
    end
    

    (or similar)