2013-11-23 37 views
2

雖然這不是CouchDB特定的,但我正在使用的庫是,它是。我在Ruby中使用couchrest_model創建User模型像這樣如下:如何使用CouchRest和BCrypt將密碼存儲在CouchDB中?

class User < CouchRest::Model::Base 
    use_database 'sample' 
    property :_id, String 
    property :email, String 
    # property :password 
    timestamps! 
end 

我很困惑與存儲在這裏的密碼。我想使用BCrypt但是當我做這樣的事情:

class User < CouchRest::Model::Base 
    include BCrypt 

    use_database 'sample' 
    property :_id, String 
    property :email, String 
    property :password, BCryptHash 

    timestamps! 
end 

我得到告訴User::BCryptHash是一個未初始化的常數。我當然需要預先bcrypt庫。你能幫我解決BCrypt問題,或者提出一種在CouchDB中存儲密碼的不同方式嗎?我已經看過hashing passwords但是我不知道如何實現這一點。

回答

1
require 'couchrest_model' 

class User < CouchRest::Model::Base 
    include ActiveModel::SecurePassword 

    use_database 'sample' 
    has_secure_password 

    property :username, String 
    property :email, String 
    property :password_digest, String 

    timestamps! 

    design { view :by_email } 
end 

User.create(:username => 'rafalchmiel', :email => '[email protected]', :password => 'password', :password_confirmation => 'password') 
User.create(:username => 'bar', :email => '[email protected]', :password => 'password213', :password_confirmation => 'password213') 
User.create(:username => 'foo', :email => '[email protected]', :password => 'password12111', :password_confirmation => 'password12111') 

更多信息爲什麼這項工作在this GitHub Issue

相關問題