Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using BCrypt #177

Closed
ghost opened this issue Nov 23, 2013 · 7 comments
Closed

Using BCrypt #177

ghost opened this issue Nov 23, 2013 · 7 comments

Comments

@ghost
Copy link

ghost commented Nov 23, 2013

I'm not sure how to approach password storing in CouchDB. I'd like to use BCrypt and therefore do something like this:

require 'couchrest_model'
require 'bcrypt'

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

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

  timestamps!
end

However that tells me that User::BCryptHash is an uninitialized constant. Any way to make this work or am I just doing it wrong?

@jhecking
Copy link

Have you tried using ActiveModel's has_secure_password method?

Something like this should work (untested):

class User < CouchRest::Model::Base
  use_database "sample"
  timestamps!
  has_secure_password

  property :email, String
  property :password_digest, String

  design do
    view :by_email
  end
end

You use it the same way as in the ActiveModel::SecurePassword documentation:

user = User.new(email: 'david', password: '', password_confirmation: 'nomatch')
user.save                                                       # => false, password required
user.password = 'mUc3m00RsqyRe'
user.save                                                       # => false, confirmation doesn't match
user.password_confirmation = 'mUc3m00RsqyRe'
user.save                                                       # => true
user.authenticate('notright')                                   # => false
user.authenticate('mUc3m00RsqyRe')                              # => user
User.find_by_email('david').try(:authenticate, 'notright')      # => false
User.find_by_email('david').try(:authenticate, 'mUc3m00RsqyRe') # => user

@ghost
Copy link
Author

ghost commented Nov 23, 2013

Thanks @jhecking, I think that's what I'm looking for however it seems to be telling me undefined local variable or method 'has_secure_password' for User:Class (NameError). Maybe because ActiveModel::SecurePassword isn't required in lib/couchrest_model.rb?

@jhecking
Copy link

Sorry, looks like you need to include ActiveModel::SecurePassword in your model class as well.

@ghost
Copy link
Author

ghost commented Nov 23, 2013

Awesome, thanks!

@ghost ghost closed this as completed Nov 23, 2013
@ghost ghost reopened this Nov 23, 2013
@ghost
Copy link
Author

ghost commented Nov 23, 2013

I just noticed that by creating some new users, they don't appear in the actual DB (I'm viewing it in Futon). I have the following code:

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

After running it, no documents are inserted. However if I run all this except leave out has_secure_password, everything works fine; the DB is created if not already and three new documents are inserted. Any ideas as to why this is happening?

@jhecking
Copy link

has_secure_password adds a validation to your model that requires that you pass a password_confirmation property whenever you set or change the password.

Jan

On 24 Nov, 2013, at 2:59 am, Rafal Chmiel notifications@github.com wrote:

I just noticed that by creating some new users, they don't appear in the actual DB (I'm viewing it in Futon). I have the following code:

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')
User.create(:username => 'bar', :email => 'hi@bar.com', :password => 'password213')
User.create(:username => 'foo', :email => 'hi@foo.com', :password => 'password12111')
After running it, no documents are inserted. However if I run all this except leave out has_secure_password, everything works fine; the DB is created if not already and three new documents are inserted. Any ideas as to why this is happening?


Reply to this email directly or view it on GitHub.

@ghost
Copy link
Author

ghost commented Nov 24, 2013

Super, thanks a lot!

@ghost ghost closed this as completed Nov 24, 2013
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant