Created
September 19, 2012 20:07
-
-
Save brenoperucchi/3751925 to your computer and use it in GitHub Desktop.
validate password devise 1.0.x to 2.x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#First you need to copy password_salt and encrypted_password to your new object model | |
#Using this because I have to export my database User to another application and old, | |
# app are using devise 1.0.x and new app using 2.1.x | |
Class User < ActiveRecord::Base | |
alias :devise_valid_password? :valid_password? | |
def valid_password?(password) | |
begin | |
devise_valid_password?(password) | |
rescue BCrypt::Errors::InvalidHash | |
salt = password_salt | |
digest = nil | |
10.times { digest = ::Digest::SHA1.hexdigest('--' << [salt, digest, password, nil].flatten.join('--') << '--') } | |
digest | |
return false unless digest == encrypted_password | |
logger.info "User #{email} is using the old password hashing method, updating attribute." | |
self.password = password | |
self.password_salt = nil # With this you will knew what object already using the new authentication by devise | |
self.save | |
true | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment