Skip to content

Instantly share code, notes, and snippets.

@fellix
Created August 29, 2011 15:08
Show Gist options
  • Select an option

  • Save fellix/1178571 to your computer and use it in GitHub Desktop.

Select an option

Save fellix/1178571 to your computer and use it in GitHub Desktop.
Has Secure Password
# Schema: User(name:string, password_digest:string, password_salt:string)
class User < ActiveRecord::Base
has_secure_password
end
user = User.new(:name => "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_name("david").try(:authenticate, "notright") # => nil
User.find_by_name("david").try(:authenticate, "mUc3m00RsqyRe") # => user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment