将密码迁移到Devise

我是从PHP到Rails的迁移用户数据库。 我已经安装了Devise Gem,它现在运行良好。 另外,我发现了如何将现有用户的密码迁移到Rails的提示我已经将旧密码添加到与Devise保持相同的encrypted_password字段中,因此当设计无法validation时,检查旧密码:

 # user.rb def valid_password?(password) return false if encrypted_password.blank? require 'digest/sha1' password_salt = 'my_php_framework_salt' Devise.secure_compare(Digest::SHA1.hexdigest(password_salt+password), self.encrypted_password) end 

它允许使用旧密码登录,但不适用于新用户的原始Devise密码。 我认为这个valid_password? 对于设计密码,方法应该return true 。 如何解决这个问题?

我相信如果您手动将'my_php_framework_salt'填入旧用户的数据库行,那么只需使用:

self.password_salt而不是password_salt它会工作。

我记得ruby返回函数中最后一行的结果。 Devise.secure_compare应该返回一个bool,这意味着valid_password? 也会返回一个布尔值。

简而言之:

 require 'digest/sha1' # ... def valid_password?(password) return false if encrypted_password.blank? Devise.secure_compare(Digest::SHA1.hexdigest(self.password_salt+password), self.encrypted_password) end