Permalink
Browse files

save a encrypted password

  • Loading branch information...
1 parent 53c8299 commit 0352486d5996cfd21fbd1b7957188decd046181f @naoty committed Jul 16, 2012
Showing with 14 additions and 0 deletions.
  1. +14 −0 app/models/user.rb
View
@@ -3,8 +3,22 @@ class User
include Mongoid::Timestamps
field :name
+ field :encrypted_password
field :_id, type: String, default: -> { name }
+ attr_accessor :password, :salt
+
index({ name: 1 }, { unique: true })
validates :name, presence: true, uniqueness: true
+
+ before_save :encrypt_password
+
+ private
+
+ def encrypt_password
+ if @password.present?
+ @salt = BCrypt::Engine.generate_salt
+ self.encrypted_password = BCrypt::Engine.hash_secret(@password, @salt)
+ end
+ end
end

0 comments on commit 0352486

Please sign in to comment.