Simple, complete Ruby web app authentication.
In config/environments/test.rb:
config.gem ‘thoughtbot-shoulda’, :lib => ‘shoulda’, :source => “http://gems.github.com” config.gem ‘thoughtbot-factory_girl’, :lib => ‘factory_girl’, :source => “http://gems.github.com”In config/environment.rb:
config.gem “thoughtbot-clearance”, :lib => ‘clearance’, :source => ‘http://gems.github.com/’Then:
rake gems:install rake gems:unpackIn a greenfield application, just run the generator:
script/generate clearanceThis will create:
app/controllers/confirmations_controller.rb app/controllers/passwords_controller.rb app/controllers/sessions_controller.rb app/controllers/users_controller.rb app/models/user.rb app/models/user_mailer.rb app/views/confirmations/new.html.haml app/views/passwords/edit.html.haml app/views/passwords/new.html.haml app/views/sessions/new.html.haml app/views/user_mailer/change_password.html.haml app/views/user_mailer/confirmation.html.haml app/views/users/_form.html.haml app/views/users/edit.html.haml app/views/users/new.html.haml test/functional/confirmations_controller_test.rb test/functional/passwords_controller_test.rb test/functional/sessions_controller_test.rb test/functional/users_controller_test.rb test/unit/user_mailer_test.rb test/unit/user_test.rbAdd the corresponding Clearance module for any file(s) you don’t want to override. They are namespaced exactly like the directory structure of a Rails app:
app/models/user.rb already exists. include Clearance::App::Models::UserThe tests use Shoulda >= 2.0.4 and Factory Girl. You should create a User Factory:
Factory.sequence :email do |n| “user#{n}@example.com” end Factory.define :user do |user| user.email { Factory.next :email } user.password “password” user.password_confirmation “password” endIn test/test_helper.rb:
class Test::Unit::TestCase self.use_transactional_fixtures = true self.use_instantiated_fixtures = false include Clearance::Test::TestHelper endIn app/controllers/application_controller.rb:
class ApplicationController < ActionController::Base helper :all protect_from_forgery include Clearance::App::Controllers::ApplicationController endIn app/controllers/sessions_controller.rb:
class SessionsController < ApplicationController include Clearance::App::Controllers::SessionsController include Clearance::App::Controllers::FacebookSessionsController facebook_to_user_field_mappings \ :first_name => :first_name, :last_name => :last_name endYour users table needs a few columns.
create_table(:users) do |t| t.string :facebook_id t.string :first_name t.string :last_name t.string :email t.string :crypted_password, :limit => 40 t.string :salt, :limit => 40 t.string :remember_token t.datetime :remember_token_expires_at t.boolean :confirmed, :default => false, :null => false t.string :confirmation_code t.string :reset_password_code end add_index :users, :email add_index :users, :facebook_id add_index :users, :remember_token map.resources :users map.resource :session map.resources :users, :has_one => :password map.resources :users, :has_one => :confirmation map.resources :passwords map.root :controller => ‘sessions’, :action => ‘new’ If you want facebook integration map.resource :session, :member => { :facebook_new => :get, :facebook_create => :get }In config/environments/test.rb and config/environments/development.rb:
HOST = “localhost”In config/environment.rb:
DO_NOT_REPLY = “donotreply@example.com” PROJECT_NAME = “my_app_name”In order to use the Facebook sign-in features, you need to install the Facebooker plugin.
./script/plugin install git://github.com/mmangino/facebooker.gitTODO: talk about disabliing Facebook features by removing includes
- thoughtbot, inc.
- Dan Croak
- Jason Morrison
- Mike Burns
- Josh Nichols
- Mike Breen
- Hashrocket, Inc.
- Les Hill
- Jon “Lark” Larkowski
- Wes Gibbs