module Devise::Controllers::Helpers
def self.included(base)
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 default_url_options(options=nil) Devise::Mapping.default_url_options end end end
def after_sign_in_path_for(resource_or_scope)
end
end
super
else
publisher_url
if resource.is_a?(User) && resource.can_publish?
def after_sign_in_path_for(resource)
is not enough, you can customize it, for example:
If none of these are defined, root_path is used. However, if this default
end
users.root # creates user_root_path
map.resources :users do |users|
map.user_root '/users', :controller => 'users' # creates user_root_path
the following way:
uses the root path. For a user scope, you can define the default url in
By default, it first tries to find a resource_root_path, otherwise it
provide a custom hook for a custom resource.
controllers and you can overwrite it in your ApplicationController to
The default url to be used after signing in. This is used by all Devise
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
def after_sign_out_path_for(resource_or_scope)
receives a symbol with the scope, and not the resource.
scope. Notice that differently from +after_sign_in_path_for+ this method
it in your ApplicationController to provide a custom hook for a custom
Method used by sessions controller to sign out an user. You can overwrite
def after_sign_out_path_for(resource_or_scope) root_path end
def anybody_signed_in?
Check if the any scope is signed in session, without running
def anybody_signed_in? Devise.mappings.keys.any? { |scope| signed_in?(scope) } end
def authenticate(scope)
Attempts to authenticate the given scope by running authentication hooks,
def authenticate(scope) warden.authenticate(:scope => scope) end
def authenticate!(scope)
Attempts to authenticate the given scope by running authentication hooks,
def authenticate!(scope) warden.authenticate!(:scope => scope) end
def default_url_options(options=nil)
Use devise default_url_options. We have to declare it here to overwrite
def default_url_options(options=nil) Devise::Mapping.default_url_options end
def devise_controller?
filter to all controller, except the ones in devise:
the controllers defined inside devise. Useful if you want to apply a before
Return true if it's a devise_controller. false to all controllers unless
def devise_controller? false end
def sign_in(resource_or_scope, resource=nil)
sign_in @user # sign_in(resource)
sign_in :user, @user # sign_in(scope, resource)
Examples:
users in after sign up.
Sign in an user that already was authenticated. This helper is useful for logging
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
def sign_in_and_redirect(resource_or_scope, resource=nil, skip=false)
If just a symbol is given, consider that the user was already signed in
then to the url specified by after_sign_in_path_for.
Sign in an user and tries to redirect first to the stored location and
def sign_in_and_redirect(resource_or_scope, resource=nil, skip=false) scope = Devise::Mapping.find_scope!(resource_or_scope) resource ||= resource_or_scope sign_in(scope, resource) unless skip redirect_to stored_location_for(scope) || after_sign_in_path_for(resource) end
def sign_out(resource_or_scope)
sign_out @user # sign_out(resource)
sign_out :user # sign_out(scope)
Examples:
after deleting accounts.
Sign out a given user or scope. This helper is useful for signing out an user
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
def sign_out_and_redirect(resource_or_scope)
Sign out an user and tries to redirect to the url specified by
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
def signed_in?(scope)
Check if the given scope is signed in session, without running
def signed_in?(scope) warden.authenticate?(:scope => scope) end
def stored_location_for(resource_or_scope)
redirect_to stored_location_for(:user) || root_path
Example:
for giving redirect backs after sign up:
Returns and delete the url stored in the session for the given scope. Useful
def stored_location_for(resource_or_scope) scope = Devise::Mapping.find_scope!(resource_or_scope) session.delete(:"#{scope}.return_to") end
def warden
def warden request.env['warden'] end