Question

Although, this isn't CouchDB specific, the library I'm using is. I'm using couchrest_model in Ruby to create a User model like this below:

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

I'm confused with password storing here. I'd like to use BCrypt but when I do something like this:

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

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

  timestamps!
end

I get told that User::BCryptHash is an uninitialised constant. I of course require the bcrypt library beforehand. Could you help me resolve that BCrypt issue or suggest a different way of storing passwords in CouchDB? I've looked at hashing passwords however I'm not sure how to implement this.

Was it helpful?

Solution

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 => 'hi@rafalchmiel.com', :password => 'password', :password_confirmation => 'password')
User.create(:username => 'bar', :email => 'hi@bar.com', :password => 'password213', :password_confirmation => 'password213')
User.create(:username => 'foo', :email => 'hi@foo.com', :password => 'password12111', :password_confirmation => 'password12111')

More info as to why this works in this GitHub Issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top