0
私は検証スキームを実装しており、bcrypt-ruby gemを使っています。bcrypt-rubyの使い方が混乱しています
class User < ActiveRecord::Base
include BCrypt
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation
validates :password, :presence => true, :on => :create,
:confirmation => true,
:length => {:within => 6..12}
before_save :encrypt_password
def has_password?(submitted_password)
self.encrypted_password == submitted_password # this calls a method in bcrypt
# File lib/bcrypt.rb, line 171
# def ==(secret)
# super(BCrypt::Engine.hash_secret(secret, @salt))
# end
end
private
def encrypt_password
self.encrypted_password = Password.create(password, :cost => 5)
end
end
今コンソールに、私は新しいユーザー
>> user = User.create!(:name => "test", :email => "[email protected]", :password => "foobar", :password_confirmation => "foobar")
=> #<User id: 1, name: "test", email: "[email protected]", created_at: "2011-06-23 05:00:00", updated_at: "2011-06-23 05:00:00", encrypted_password: "$2a$10$I7Wy8NDMeVcNgOsE3J/ZyubiNAESyxA7Z49H4p1x5xxH...">
を作成し、パスワードが有効であれば、私はチェックすると、私は次の操作を行い「bcryptの」を必要とします。
>> user.has_password?("foobar")
=> true
データベースからユーザーを取得すると、失敗します。
user = User.find(1)
user.has_password?("foobar")
=> false
なぜこのようなことが起こるのですか?bcryptを実装するにはどうしたらいいですか?
ありがとうございます。
encypted_passwordではなくdbにBCrypt :: Passwordを格納する方が良いですか?どうやってやるの? – chell