Module: Devise::Controllers::Helpers
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/devise/controllers/helpers.rb
Overview
Those helpers are convenience methods added to ApplicationController.
Instance Method Summary collapse
-
#after_sign_in_path_for(resource_or_scope) ⇒ Object
The default url to be used after signing in.
-
#after_sign_out_path_for(resource_or_scope) ⇒ Object
Method used by sessions controller to sign out an user.
-
#after_update_path_for(resource_or_scope) ⇒ Object
The default url to be used after updating a resource.
-
#anybody_signed_in? ⇒ Boolean
Check if the any scope is signed in session, without running authentication hooks.
-
#devise_controller? ⇒ Boolean
Return true if it’s a devise_controller.
-
#sign_in(resource_or_scope, resource = nil) ⇒ Object
Sign in an user that already was authenticated.
-
#sign_in_and_redirect(resource_or_scope, resource = nil) ⇒ Object
Sign in an user and tries to redirect first to the stored location and then to the url specified by after_sign_in_path_for.
-
#sign_out(resource_or_scope) ⇒ Object
Sign out a given user or scope.
-
#sign_out_all_scopes ⇒ Object
Sign out all active users or scopes.
-
#sign_out_and_redirect(resource_or_scope) ⇒ Object
Sign out an user and tries to redirect to the url specified by after_sign_out_path_for.
-
#signed_in?(scope) ⇒ Boolean
Check if the given scope is signed in session, without running authentication hooks.
-
#stored_location_for(resource_or_scope) ⇒ Object
Returns and delete the url stored in the session for the given scope.
-
#warden ⇒ Object
The main accessor for the warden proxy instance.
Instance Method Details
#after_sign_in_path_for(resource_or_scope) ⇒ Object
The default url to be used after signing in. This is used by all Devise controllers and you can overwrite it in your ApplicationController to provide a custom hook for a custom resource.
By default, it first tries to find a resource_root_path, otherwise it uses the root path. For a user scope, you can define the default url in the following way:
map.user_root '/users', :controller => 'users' # creates user_root_path
map.namespace :user do |user|
user.root :controller => 'users' # creates user_root_path
end
If the resource root path is not defined, root_path is used. However, if this default is not enough, you can customize it, for example:
def after_sign_in_path_for(resource)
if resource.is_a?(User) && resource.can_publish?
publisher_url
else
super
end
end
115 116 117 118 119 |
# File 'lib/devise/controllers/helpers.rb', line 115 def after_sign_in_path_for(resource_or_scope) scope = Devise::Mapping.find_scope!(resource_or_scope) home_path = "#{scope}_root_path" respond_to?(home_path, true) ? send(home_path) : root_path end |
#after_sign_out_path_for(resource_or_scope) ⇒ Object
Method used by sessions controller to sign out an user. You can overwrite it in your ApplicationController to provide a custom hook for a custom scope. Notice that differently from after_sign_in_path_for
this method receives a symbol with the scope, and not the resource.
By default is the root_path.
157 158 159 |
# File 'lib/devise/controllers/helpers.rb', line 157 def after_sign_out_path_for(resource_or_scope) root_path end |
#after_update_path_for(resource_or_scope) ⇒ Object
The default url to be used after updating a resource. This is used by all Devise controllers and you can overwrite it in your ApplicationController to provide a custom hook for a custom resource.
By default, it first tries to find a resource_root_path, otherwise it uses the root path. For a user scope, you can define the default url in the following way:
map.user_root '/users', :controller => 'users' # creates user_root_path
map.resources :users do |users|
users.root # creates user_root_path
end
If none of these are defined, root_path is used. However, if this default is not enough, you can customize it, for example:
def after_update_path_for(resource)
if resource.is_a?(User) && resource.can_publish?
publisher_url
else
super
end
end
147 148 149 |
# File 'lib/devise/controllers/helpers.rb', line 147 def after_update_path_for(resource_or_scope) after_sign_in_path_for(resource_or_scope) end |
#anybody_signed_in? ⇒ Boolean
Check if the any scope is signed in session, without running authentication hooks.
34 35 36 |
# File 'lib/devise/controllers/helpers.rb', line 34 def anybody_signed_in? Devise.mappings.keys.any? { |scope| signed_in?(scope) } end |
#devise_controller? ⇒ Boolean
Return true if it’s a devise_controller. false to all controllers unless the controllers defined inside devise. Useful if you want to apply a before filter to all controller, except the ones in devise:
before_filter :my_filter, :unless => { |c| c.devise_controller? }
22 23 24 |
# File 'lib/devise/controllers/helpers.rb', line 22 def devise_controller? false end |
#sign_in(resource_or_scope, resource = nil) ⇒ Object
Sign in an user that already was authenticated. This helper is useful for logging users in after sign up.
Examples:
sign_in :user, @user # sign_in(scope, resource)
sign_in @user # sign_in(resource)
46 47 48 49 50 |
# File 'lib/devise/controllers/helpers.rb', line 46 def sign_in(resource_or_scope, resource=nil) scope = Devise::Mapping.find_scope!(resource_or_scope) resource ||= resource_or_scope warden.set_user(resource, :scope => scope) end |
#sign_in_and_redirect(resource_or_scope, resource = nil) ⇒ Object
Sign in an user and tries to redirect first to the stored location and then to the url specified by after_sign_in_path_for.
If just a symbol is given, consider that the user was already signed in through other means and just perform the redirection.
166 167 168 169 170 171 |
# File 'lib/devise/controllers/helpers.rb', line 166 def sign_in_and_redirect(resource_or_scope, resource=nil) scope = Devise::Mapping.find_scope!(resource_or_scope) resource ||= resource_or_scope sign_in(scope, resource) unless warden.user(scope) == resource redirect_to stored_location_for(scope) || after_sign_in_path_for(resource) end |
#sign_out(resource_or_scope) ⇒ Object
Sign out a given user or scope. This helper is useful for signing out an user after deleting accounts.
Examples:
sign_out :user # sign_out(scope)
sign_out @user # sign_out(resource)
60 61 62 63 64 65 |
# File 'lib/devise/controllers/helpers.rb', line 60 def sign_out(resource_or_scope) scope = Devise::Mapping.find_scope!(resource_or_scope) warden.user(scope) # Without loading user here, before_logout hook is not called warden.raw_session.inspect # Without this inspect here. The session does not clear. warden.logout(scope) end |
#sign_out_all_scopes ⇒ Object
Sign out all active users or scopes. This helper is useful for signing out all roles in one click.
69 70 71 72 73 74 75 |
# File 'lib/devise/controllers/helpers.rb', line 69 def sign_out_all_scopes # Not "warden.logout" since we need to sign_out only devise-defined scopes. scopes = Devise.mappings.keys scopes.each { |scope| warden.user(scope) } warden.raw_session.inspect warden.logout(*scopes) end |
#sign_out_and_redirect(resource_or_scope) ⇒ Object
Sign out an user and tries to redirect to the url specified by after_sign_out_path_for.
175 176 177 178 179 180 181 182 183 |
# File 'lib/devise/controllers/helpers.rb', line 175 def sign_out_and_redirect(resource_or_scope) scope = Devise::Mapping.find_scope!(resource_or_scope) if Devise.sign_out_all_scopes sign_out_all_scopes else sign_out(scope) end redirect_to after_sign_out_path_for(scope) end |
#signed_in?(scope) ⇒ Boolean
Check if the given scope is signed in session, without running authentication hooks.
28 29 30 |
# File 'lib/devise/controllers/helpers.rb', line 28 def signed_in?(scope) warden.authenticate?(:scope => scope) end |
#stored_location_for(resource_or_scope) ⇒ Object
Returns and delete the url stored in the session for the given scope. Useful for giving redirect backs after sign up:
Example:
redirect_to stored_location_for(:user) || root_path
84 85 86 87 |
# File 'lib/devise/controllers/helpers.rb', line 84 def stored_location_for(resource_or_scope) scope = Devise::Mapping.find_scope!(resource_or_scope) session.delete("#{scope}_return_to") end |
#warden ⇒ Object
The main accessor for the warden proxy instance
13 14 15 |
# File 'lib/devise/controllers/helpers.rb', line 13 def warden request.env['warden'] end |