1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Allow page after sign in to be configured.

This commit is contained in:
José Valim 2009-10-18 15:25:16 -02:00
parent b0a2da72b5
commit 9feb9455ce
5 changed files with 39 additions and 16 deletions

View file

@ -16,7 +16,7 @@ Right now it's composed of four mainly modules:
== Dependencies == 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 == Installation
@ -28,7 +28,7 @@ Install devise as an engine (plugin) inside your app:
script/plugin install git://github.com/plataformatec/devise.git script/plugin install git://github.com/plataformatec/devise.git
And you're ready to use devise. And you're ready to go.
== Basic Usage == Basic Usage
@ -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 :email, :null => false
t.string :encrypted_password, :null => false t.string :encrypted_password, :null => false
t.string :password_salt, :null => false t.string :password_salt, :null => false
# required for confirmable # required for confirmable
t.string :confirmation_token t.string :confirmation_token
t.datetime :confirmation_sent_at t.datetime :confirmation_sent_at
t.datetime :confirmed_at t.datetime :confirmed_at
# required for recoverable # required for recoverable
t.string :reset_password_token t.string :reset_password_token
@ -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 # Same as using only devise, authenticable is activated by default
devise :authenticable devise :authenticable
# Include confirmable
# Include authenticable + confirmable
devise :confirmable devise :confirmable
# Include recoverable
# Include authenticable + recoverable
devise :recoverable devise :recoverable
# Include validatable
devise :validatable # Include authenticable + conformable + recoverable + validatable
# Include all of them
devise :confirmable, :recoverable, :validatable devise :confirmable, :recoverable, :validatable
# Same as above, include all of them # Same as above, include all of them
devise :all 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. 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: 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:
@ -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: 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: To verify if a user is signed in, you have the following helper:
@ -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 :email, :null => false
t.string :encrypted_password, :null => false t.string :encrypted_password, :null => false
t.string :password_salt, :null => false t.string :password_salt, :null => false
# Inside your Admin model # Inside your Admin model
devise :validatable devise :validatable
# Inside your routes # Inside your routes
map.devise_for :admin map.devise_for :admin
# Inside your protected controller # Inside your protected controller
before_filter :sign_in_admin! before_filter :sign_in_admin!
# Inside your controllers and views # Inside your controllers and views
admin_signed_in? admin_signed_in?
current_admin current_admin

View file

@ -11,7 +11,7 @@ class SessionsController < ApplicationController
def create def create
if authenticate(resource_name) if authenticate(resource_name)
set_flash_message :success, :signed_in set_flash_message :success, :signed_in
redirect_back_or_to root_path redirect_back_or_to home_or_root_path
else else
unauthenticated! unauthenticated!
render :new render :new
@ -31,4 +31,9 @@ class SessionsController < ApplicationController
flash.now[:failure] = I18n.t(:"#{resource_name}.unauthenticated", flash.now[:failure] = I18n.t(:"#{resource_name}.unauthenticated",
:scope => [:devise, :sessions], :default => :unauthenticated) :scope => [:devise, :sessions], :default => :unauthenticated)
end end
def home_or_root_path
home_path = :"#{resource_name}_home_path"
respond_to?(home_path, true) ? send(home_path) : root_path
end
end end

View file

@ -16,13 +16,13 @@ module Devise
# # include authenticable + validatable modules # # include authenticable + validatable modules
# devise :validatable # devise :validatable
# #
# # include all modules # # include authenticable + confirmable + recoverable + validatable
# devise :confirmable, :recoverable, :validatable # devise :confirmable, :recoverable, :validatable
# #
# # shortcut to include all modules (same as above) # # shortcut to include all modules (same as above)
# devise :all # devise :all
# #
# # include all except :recoverable # # include all except recoverable
# devise :all, :except => :recoverable # devise :all, :except => :recoverable
# #
def devise(*modules) def devise(*modules)

View file

@ -2,7 +2,7 @@ require 'test/test_helper'
class AuthenticationTest < ActionController::IntegrationTest class AuthenticationTest < ActionController::IntegrationTest
test 'home should be accessible without signed in admins' do test 'home should be accessible without signed in' do
visit '/' visit '/'
assert_response :success assert_response :success
assert_template 'home/index' assert_template 'home/index'
@ -64,7 +64,7 @@ class AuthenticationTest < ActionController::IntegrationTest
assert_redirected_to new_admin_session_path(:unauthenticated => true) assert_redirected_to new_admin_session_path(:unauthenticated => true)
end 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 sign_in_as_admin
assert warden.authenticated?(:admin) assert warden.authenticated?(:admin)
assert_not warden.authenticated?(:user) assert_not warden.authenticated?(:user)
@ -135,7 +135,7 @@ class AuthenticationTest < ActionController::IntegrationTest
assert_not warden.authenticated?(:admin) assert_not warden.authenticated?(:admin)
end 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 get destroy_admin_session_path
assert_response :redirect assert_response :redirect
assert_redirected_to root_path assert_redirected_to root_path
@ -144,7 +144,7 @@ class AuthenticationTest < ActionController::IntegrationTest
assert_not_contain 'Signed out successfully' assert_not_contain 'Signed out successfully'
end end
test 'redirect with warden show error message' do test 'redirect from warden shows error message' do
get admins_path get admins_path
warden_path = new_admin_session_path(:unauthenticated => true) warden_path = new_admin_session_path(:unauthenticated => true)
@ -160,7 +160,7 @@ class AuthenticationTest < ActionController::IntegrationTest
assert_not_contain 'Send me reset password instructions' assert_not_contain 'Send me reset password instructions'
end 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 sign_in_as_user
assert_template 'home/index' assert_template 'home/index'
@ -178,6 +178,11 @@ class AuthenticationTest < ActionController::IntegrationTest
assert_nil session[:"user.return_to"] assert_nil session[:"user.return_to"]
end 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 test 'allows session to be set by a given scope' do
sign_in_as_user sign_in_as_user
visit 'users/index' visit 'users/index'

View file

@ -10,6 +10,8 @@ ActionController::Routing::Routes.draw do |map|
map.root :controller => :home map.root :controller => :home
map.connect '/admin_area/password/new', :controller => "passwords", :action => "new" 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'
map.connect ':controller/:action/:id.:format' map.connect ':controller/:action/:id.:format'
end end