Additional Authlogic Password Confirmation

Is it possible that AuthLogic will confirm the password confirmation if it is present, but otherwise ignores it? So, if my parameters are as follows, I would like to crash:

{ user: { password: "abcd", password_confirmation: "defg" }

However, if the options follow instead, I would like to succeed:

{ user: { password: "abcdefgh" } }

Thank!

+3
source share
2 answers

this is not an AuthLogic question, this code should do the trick:

class User < ActiveRecord::Base
  ...

  def update_without_password_confirmation(params={})
    params.delete(:password) if params[:password_confirmation].blank?
    params.delete(:password_confirmation) if params[:password].blank?

    update_attributes(params)
   end      
end

then call this method from the controller when you need to update the attributes for the user. If the User has not provided a confirmation password, it will be ignored. you can use several other methods to get the same effect, for example, using the before_validation callback (: on =>: update).

UPDATE: , act_as_authentic :

clas User < AR::Base
  acts_as_authentic do |u|
    u.require_password_confirmation=false # you can also use :if => some_condition
    u.validate_password_field=false
  end
end

: P : https://github.com/binarylogic/authlogic/blob/master/lib/authlogic/acts_as_authentic/password.rb

+2
before_validate :check_password_confirmation
def check_password_confirmation
  if self.password_confirmation.blank?
    self.password = nil
  end
end

, - .

-1

All Articles