This is a Rails gem that I wrote, or rather extracted from the codebase from hushyhushy.com. OK, what is PersistentCookie Authentication Generator?
This gem is a code generator. This generator creates an authentication system with persistent cookie management.
Feature include
- a model which uses SHA1 encryption and salted hashes for passwords
- a controller with signup, login, welcome and logoff actions
- gmail smtp server integration
- account creation that requires account verification via email
- supports forgotten and changed passwords
- a mixin which lets you easily add advanced authentication features to your abstract base controller
- extensive unit and functional test cases to make sure nothing breaks.
- token based authentication
- persistent cookie management that allows anonymous users to be authenticated via cookies
Actually the code is heavily modified from another well known gem: salted hash login generator
To put it simply, it is
persistent_cookie_authentication = salted_hash_login – localization + persistent_cookie_management
The code is written with an emphasis on simplicity. No effort is made from me to write clever or DRY(don’t repeat yourself) code. The intention is to dumb it down so that people using it could understand it. As Joel says, abstraction is bad.
Here’s the installation instructions:
To install, run this line on the shell
gem install persistent_cookie_authentication_generatorFrom the directory of your Rails app, run this from the shell
ruby script/generate persistent_cookie_authenticationChange your config/database.yml settings. Then update your database by running
rake db:migrate
From app/controllers/application.rb, add this within application.rb
include UserSystemExample application.rb
From config/environment.rb, add this at the end of the file
require 'environments/user_environment'
require 'smtp_tls'
Example environment.rb
From config/environments/, add this to the end of both development.rb and production.rb
ActionMailer::Base.smtp_settings = {:address => "smtp.gmail.com", :port => "587", :domain => "localhost.localdomain", :authentication => :plain, :user_name => "yourgmailusername", :password => "yourgmailpassword" }
Example development.rb
From config/initializers/inflections.rb, modify your inflections.rb to look like this
Inflector.inflections do |inflect|# inflect.plural /^(ox)$/i, '1en' # inflect.singular /^(ox)en/i, '1' # inflect.irregular 'person', 'people' # inflect.uncountable %w( fish sheep ) inflect.irregular 'LoginCookie', 'LoginCookies' end
To test, run this from your shell
rake db:test:clonerake test
From config/user_environment.rb, change the name of your Rails app to your liking
That’s it. Another post will cover how to use persistent login cookies. Bug reports please contact me directly at my email address.
*Update: Here’s the slides I’ve presented at the Singapore Ruby Meeting



Thanks for your gem. I tried it this morning on Windows, and got 13 failures in the integration tests. I could paste them all in here, but it’d be pretty long … anyway, here’s the last two. All but the last had the same “Expected response” line as number 12. Any idea what’s wrong? I’m pretty sure I followed your directions correctly.
12) Failure:
test_valid_login_same_user(UserControllerTest)
[./test/integration/user_integration_test.rb:23:in `is_redirected_to'
./test/integration/user_integration_test.rb:69:in `test_valid_login_same_user'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/integration.rb:547:in `run']:
Expected response to be a , but was
13) Failure:
test_valid_signup(UserControllerTest)
[./test/integration/user_integration_test.rb:131:in `test_valid_signup'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/integration.rb:547:in `run']:
expected but was
.
15 tests, 77 assertions, 13 failures, 0 errors
Errors running test:integration!
I see the weblog trimmed out parts of the error codes in my comment. Let’s see, does this work?
12) Failure:
test_valid_login_same_user(UserControllerTest)
[./test/integration/user_integration_test.rb:23:in `is_redirected_to'
./test/integration/user_integration_test.rb:69:in `test_valid_login_same_user'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/integration.rb:547:in `run']:
Expected response to be a , but was
13) Failure:
test_valid_signup(UserControllerTest)
[./test/integration/user_integration_test.rb:131:in `test_valid_signup'
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/integration.rb:547:in `run']:
expected but was
.
15 tests, 77 assertions, 13 failures, 0 errors
Errors running test:integration!
Sorry to clutter up your comments! One last try:
12) Failure: … Expected response to be a <:redirect>, but was <500>
13) Failure … <1> expected but was <0>.
Well, we solved the problem. Think some steps were missed…