Skip to content

Commit

Permalink
Allow page after sign in to be configured.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Oct 18, 2009
1 parent b0a2da7 commit 9feb945
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
27 changes: 19 additions & 8 deletions README.rdoc
Expand Up @@ -16,7 +16,7 @@ Right now it's composed of four mainly modules:

== Dependencies

Devise is based on Warden (http://github.com/hassox/warden), a Rack Authentication Framework from hassox (http://github.com/hassox), so you're gonna need to install this gem. Current warden version is 0.4.0. Please ensure you have it installed in order to use devise (see instalation below).
Devise is based on Warden (http://github.com/hassox/warden), a Rack Authentication Framework so you need to install it as a gem. Current warden version is 0.4.0. Please ensure you have it installed in order to use devise (see instalation below).

== Installation

Expand All @@ -28,7 +28,7 @@ Install devise as an engine (plugin) inside your app:

script/plugin install git://github.com/plataformatec/devise.git

And you're ready to use devise.
And you're ready to go.

== Basic Usage

Expand All @@ -40,10 +40,12 @@ We're assuming here you want a User model. First of all you have to setup a migr
t.string :email, :null => false
t.string :encrypted_password, :null => false
t.string :password_salt, :null => false

# required for confirmable
t.string :confirmation_token
t.datetime :confirmation_sent_at
t.datetime :confirmed_at

# required for recoverable
t.string :reset_password_token

Expand All @@ -57,17 +59,22 @@ This line adds devise authenticable automatically for you inside your User class

# Same as using only devise, authenticable is activated by default
devise :authenticable
# Include confirmable

# Include authenticable + confirmable
devise :confirmable
# Include recoverable

# Include authenticable + recoverable
devise :recoverable
# Include validatable
devise :validatable
# Include all of them

# Include authenticable + conformable + recoverable + validatable
devise :confirmable, :recoverable, :validatable

# Same as above, include all of them
devise :all

# Include all except recoverable
devise :all, :except => :recoverable

Note that validations aren't added by default, so you're able to customize it. In order to have automatic validations working just include :validatable.

The next step after setting up your model is to configure your routes for devise. You do this by opening up your config/routes.rb and adding:
Expand Down Expand Up @@ -113,7 +120,7 @@ There are also some options available for configuring your routes:

And that is it! Devise is gonna create some helpers to use inside your controllers and views. To setup a controller that needs user authentication, just add this before_filter:

before_filter :sign_in_user!
before_filter :authenticate_user!

To verify if a user is signed in, you have the following helper:

Expand All @@ -129,12 +136,16 @@ Devise let's you setup as many roles as you want, so let's say you already have
t.string :email, :null => false
t.string :encrypted_password, :null => false
t.string :password_salt, :null => false

# Inside your Admin model
devise :validatable

# Inside your routes
map.devise_for :admin

# Inside your protected controller
before_filter :sign_in_admin!

# Inside your controllers and views
admin_signed_in?
current_admin
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/sessions_controller.rb
Expand Up @@ -11,7 +11,7 @@ def new
def create
if authenticate(resource_name)
set_flash_message :success, :signed_in
redirect_back_or_to root_path
redirect_back_or_to home_or_root_path
else
unauthenticated!
render :new
Expand All @@ -31,4 +31,9 @@ def unauthenticated!
flash.now[:failure] = I18n.t(:"#{resource_name}.unauthenticated",
:scope => [:devise, :sessions], :default => :unauthenticated)
end

def home_or_root_path
home_path = :"#{resource_name}_home_path"
respond_to?(home_path, true) ? send(home_path) : root_path
end
end
4 changes: 2 additions & 2 deletions lib/devise/active_record.rb
Expand Up @@ -16,13 +16,13 @@ module ActiveRecord
# # include authenticable + validatable modules
# devise :validatable
#
# # include all modules
# # include authenticable + confirmable + recoverable + validatable
# devise :confirmable, :recoverable, :validatable
#
# # shortcut to include all modules (same as above)
# devise :all
#
# # include all except :recoverable
# # include all except recoverable
# devise :all, :except => :recoverable
#
def devise(*modules)
Expand Down
15 changes: 10 additions & 5 deletions test/integration/authenticable_test.rb
Expand Up @@ -2,7 +2,7 @@

class AuthenticationTest < ActionController::IntegrationTest

test 'home should be accessible without signed in admins' do
test 'home should be accessible without signed in' do
visit '/'
assert_response :success
assert_template 'home/index'
Expand Down Expand Up @@ -64,7 +64,7 @@ class AuthenticationTest < ActionController::IntegrationTest
assert_redirected_to new_admin_session_path(:unauthenticated => true)
end

test 'signed in as admin should be able to access admin actions successfully' do
test 'signed in as admin should be able to access admin actions' do
sign_in_as_admin
assert warden.authenticated?(:admin)
assert_not warden.authenticated?(:user)
Expand Down Expand Up @@ -135,7 +135,7 @@ class AuthenticationTest < ActionController::IntegrationTest
assert_not warden.authenticated?(:admin)
end

test 'not authenticated admin does not set error message on sign out' do
test 'unauthenticated admin does not set message on sign out' do
get destroy_admin_session_path
assert_response :redirect
assert_redirected_to root_path
Expand All @@ -144,7 +144,7 @@ class AuthenticationTest < ActionController::IntegrationTest
assert_not_contain 'Signed out successfully'
end

test 'redirect with warden show error message' do
test 'redirect from warden shows error message' do
get admins_path

warden_path = new_admin_session_path(:unauthenticated => true)
Expand All @@ -160,7 +160,7 @@ class AuthenticationTest < ActionController::IntegrationTest
assert_not_contain 'Send me reset password instructions'
end

test 'return to default url if no one was requested' do
test 'return to default url if no other was requested' do
sign_in_as_user

assert_template 'home/index'
Expand All @@ -178,6 +178,11 @@ class AuthenticationTest < ActionController::IntegrationTest
assert_nil session[:"user.return_to"]
end

test 'return to configured home path after sign in' do
sign_in_as_admin
assert_equal "/admin_area/home", @request.path
end

test 'allows session to be set by a given scope' do
sign_in_as_user
visit 'users/index'
Expand Down
2 changes: 2 additions & 0 deletions test/rails_app/config/routes.rb
Expand Up @@ -10,6 +10,8 @@
map.root :controller => :home

map.connect '/admin_area/password/new', :controller => "passwords", :action => "new"
map.admin_home '/admin_area/home', :controller => "admins", :action => "index"

map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end

0 comments on commit 9feb945

Please sign in to comment.