Module: Devise::Controllers::Helpers
- Defined in:
- lib/devise/controllers/helpers.rb
Overview
Those helpers are convenience methods added to ApplicationController.
Class Method Summary collapse
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.
-
#anybody_signed_in? ⇒ Boolean
Check if the any scope is signed in session, without running authentication hooks.
-
#authenticate(scope) ⇒ Object
Attempts to authenticate the given scope by running authentication hooks, but does not redirect in case of failures.
-
#authenticate!(scope) ⇒ Object
Attempts to authenticate the given scope by running authentication hooks, redirecting in case of failures.
-
#devise_controller? ⇒ Boolean
Return true if it’s a devise_controller.
-
#handle_unverified_request ⇒ Object
Override Rails’ handle unverified request to sign out all scopes.
-
#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, skip = false) ⇒ 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.
Class Method Details
.included(base) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/devise/controllers/helpers.rb', line 6 def self.included(base) base.class_eval do helper_method :warden, :signed_in?, :devise_controller?, :anybody_signed_in?, *Devise.mappings.keys.map { |m| [:"current_#{m}", :"#{m}_signed_in?", :"#{m}_session"] }.flatten # Use devise default_url_options. We have to declare it here to overwrite # default definitions. def (=nil) Devise::Mapping. end end end |
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
126 127 128 129 130 |
# File 'lib/devise/controllers/helpers.rb', line 126 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.
138 139 140 |
# File 'lib/devise/controllers/helpers.rb', line 138 def after_sign_out_path_for(resource_or_scope) root_path end |
#anybody_signed_in? ⇒ Boolean
Check if the any scope is signed in session, without running authentication hooks.
53 54 55 |
# File 'lib/devise/controllers/helpers.rb', line 53 def anybody_signed_in? Devise.mappings.keys.any? { |scope| signed_in?(scope) } end |
#authenticate(scope) ⇒ Object
Attempts to authenticate the given scope by running authentication hooks, but does not redirect in case of failures.
35 36 37 |
# File 'lib/devise/controllers/helpers.rb', line 35 def authenticate(scope) warden.authenticate(:scope => scope) end |
#authenticate!(scope) ⇒ Object
Attempts to authenticate the given scope by running authentication hooks, redirecting in case of failures.
41 42 43 |
# File 'lib/devise/controllers/helpers.rb', line 41 def authenticate!(scope) warden.authenticate!(:scope => 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? }
29 30 31 |
# File 'lib/devise/controllers/helpers.rb', line 29 def devise_controller? false end |
#handle_unverified_request ⇒ Object
Override Rails’ handle unverified request to sign out all scopes.
175 176 177 178 |
# File 'lib/devise/controllers/helpers.rb', line 175 def handle_unverified_request sign_out_all_scopes super # call the default behaviour which resets the session 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)
65 66 67 68 69 70 |
# File 'lib/devise/controllers/helpers.rb', line 65 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) @_session = request.session # Recalculate session end |
#sign_in_and_redirect(resource_or_scope, resource = nil, skip = false) ⇒ 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.
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/devise/controllers/helpers.rb', line 147 def sign_in_and_redirect(resource_or_scope, resource=nil, skip=false) scope = Devise::Mapping.find_scope!(resource_or_scope) resource ||= resource_or_scope if skip @_session = request.session # Recalculate session else sign_in(scope, resource) end 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)
80 81 82 83 84 85 |
# File 'lib/devise/controllers/helpers.rb', line 80 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. This signs out ALL scopes in warden.
168 169 170 171 172 |
# File 'lib/devise/controllers/helpers.rb', line 168 def sign_out_all_scopes Devise.mappings.keys.each { |s| warden.user(s) } warden.raw_session.inspect warden.logout 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.
160 161 162 163 164 |
# File 'lib/devise/controllers/helpers.rb', line 160 def sign_out_and_redirect(resource_or_scope) scope = Devise::Mapping.find_scope!(resource_or_scope) sign_out(scope) 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.
47 48 49 |
# File 'lib/devise/controllers/helpers.rb', line 47 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
94 95 96 97 98 |
# File 'lib/devise/controllers/helpers.rb', line 94 def stored_location_for(resource_or_scope) scope = Devise::Mapping.find_scope!(resource_or_scope) key = "#{scope}.return_to" session.delete(key) || session.delete(key.to_sym) end |
#warden ⇒ Object
The main accessor for the warden proxy instance
20 21 22 |
# File 'lib/devise/controllers/helpers.rb', line 20 def warden request.env['warden'] end |