module Devise::Controllers::Helpers

def self.define_helpers(mapping) #:nodoc:

:nodoc:

before_filter :authenticate_admin! # Tell devise to use :admin map
before_filter :authenticate_user! # Tell devise to use :user map
Use:

admin_session # Session data available only to the admin scope
user_session # Session data available only to the user scope
current_admin # Current signed in admin
current_user # Current signed in user
admin_signed_in? # Checks whether there is an admin signed in or not
user_signed_in? # Checks whether there is a user signed in or not
authenticate_admin! # Signs admin in or redirect
authenticate_user! # Signs user in or redirect
Generated methods:

Admin
User
Roles:

Example:
access that specific controller/action.
so you can control the scope of the user who should be signed in to
These filters should be used inside the controllers as before_filters,
Define authentication filters and accessor helpers based on mappings.
def self.define_helpers(mapping) #:nodoc:
  mapping = mapping.name
  class_eval <<-METHODS, __FILE__, __LINE__ + 1
    def authenticate_#{mapping}!(opts={})
      opts[:scope] = :#{mapping}
      warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
    end
    def #{mapping}_signed_in?
      !!current_#{mapping}
    end
    def current_#{mapping}
      @current_#{mapping} ||= warden.authenticate(:scope => :#{mapping})
    end
    def #{mapping}_session
      current_#{mapping} && warden.session(:#{mapping})
    end
  METHODS
  ActiveSupport.on_load(:action_controller) do
    helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session"
  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?
stored_location_for(resource) ||
def after_sign_in_path_for(resource)

if this default is not enough, you can customize it, for example:
If the resource root path is not defined, root_path is used. However,

end
user.root :controller => 'users' # creates user_root_path
map.namespace :user do |user|

map.user_root '/users', :controller => 'users' # creates user_root_path

the following way:
root path. For a user scope, you can define the default url in
session, then it fallbacks to resource_root_path, otherwise it uses the
By default, it first tries to find a valid resource_return_to key in the

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)
  stored_location_for(resource_or_scope) || signed_in_root_path(resource_or_scope)
end

def after_sign_out_path_for(resource_or_scope)

By default it is the root_path.

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 a user. You can overwrite
def after_sign_out_path_for(resource_or_scope)
  respond_to?(:root_path) ? root_path : "/"
end

def allow_params_authentication!

Tell warden that params authentication is allowed for that specific page.
def allow_params_authentication!
  request.env["devise.allow_params_authentication"] = true
end

def devise_controller?

before_filter :my_filter, :unless => :devise_controller?

filter to all controllers, 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?
  is_a?(DeviseController)
end

def expire_devise_cached_variables!

def expire_devise_cached_variables!
  Devise.mappings.each { |_,m| instance_variable_set("@current_#{m.name}", nil) }
end

def expire_session_data_after_sign_in!

def expire_session_data_after_sign_in!
  session.keys.grep(/^devise\./).each { |k| session.delete(k) }
end

def handle_unverified_request

clear run strategies and remove cached variables.
Overwrite Rails' handle unverified request to sign out all scopes,
def handle_unverified_request
  sign_out_all_scopes(false)
  request.env["devise.skip_storage"] = true
  expire_devise_cached_variables!
  super # call the default behaviour which resets the session
end

def is_navigational_format?

def is_navigational_format?
  Devise.navigational_formats.include?(request_format)
end

def request_format

def request_format
  @request_format ||= request.format.try(:ref)
end

def sign_in(resource_or_scope, *args)


sign_in @user, :bypass => true # sign_in(resource, options)
sign_in @user, :event => :authentication # sign_in(resource, options)
sign_in @user # sign_in(resource)
sign_in :user, @user # sign_in(scope, resource)

Examples:

signed in, but we want to refresh the credentials in session.
the user straight in session. This option is useful in cases the user is already
The only exception is the :bypass option, which bypass warden callbacks and stores
All options given to sign_in is passed forward to the set_user method in warden.

users in after sign up.
Sign in a user that already was authenticated. This helper is useful for logging
def sign_in(resource_or_scope, *args)
  options  = args.extract_options!
  scope    = Devise::Mapping.find_scope!(resource_or_scope)
  resource = args.last || resource_or_scope
  expire_session_data_after_sign_in!
  if options[:bypass]
    warden.session_serializer.store(resource, scope)
  elsif warden.user(scope) == resource && !options.delete(:force)
    # Do nothing. User already signed in and we are not forcing it.
    true
  else
    warden.set_user(resource, options.merge!(:scope => scope))
  end
end

def sign_in_and_redirect(resource_or_scope, *args)

parameters as the sign_in method.
then to the url specified by after_sign_in_path_for. It accepts the same
Sign in a user and tries to redirect first to the stored location and
def sign_in_and_redirect(resource_or_scope, *args)
  options  = args.extract_options!
  scope    = Devise::Mapping.find_scope!(resource_or_scope)
  resource = args.last || resource_or_scope
  sign_in(scope, resource, options)
  redirect_to after_sign_in_path_for(resource)
end

def sign_out(resource_or_scope=nil)


sign_out @user # sign_out(resource)
sign_out :user # sign_out(scope)

Examples:

is no user logged in on the referred scope
after deleting accounts. Returns true if there was a logout and false if there
Sign out a given user or scope. This helper is useful for signing out a user
def sign_out(resource_or_scope=nil)
  return sign_out_all_scopes unless resource_or_scope
  scope = Devise::Mapping.find_scope!(resource_or_scope)
  user = warden.user(:scope => scope, :run_callbacks => false) # If there is no user
  warden.raw_session.inspect # Without this inspect here. The session does not clear.
  warden.logout(scope)
  warden.clear_strategies_cache!(:scope => scope)
  instance_variable_set(:"@current_#{scope}", nil)
  !!user
end

def sign_out_all_scopes(lock=true)

and false if there was no user logged in on all scopes.
in one click. This signs out ALL scopes in warden. Returns true if there was at least one logout
Sign out all active users or scopes. This helper is useful for signing out all roles
def sign_out_all_scopes(lock=true)
  users = Devise.mappings.keys.map { |s| warden.user(:scope => s, :run_callbacks => false) }
  warden.raw_session.inspect
  warden.logout
  expire_devise_cached_variables!
  warden.clear_strategies_cache!
  warden.lock! if lock
  users.any?
end

def sign_out_and_redirect(resource_or_scope)

after_sign_out_path_for.
Sign out a 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)
  redirect_path = after_sign_out_path_for(scope)
  Devise.sign_out_all_scopes ? sign_out : sign_out(scope)
  redirect_to redirect_path
end

def signed_in?(scope=nil)

true if any scope is signed in. Does not run authentication hooks.
Return true if the given scope is signed in session. If no scope given, return
def signed_in?(scope=nil)
  [ scope || Devise.mappings.keys ].flatten.any? do |_scope|
    warden.authenticate?(:scope => _scope)
  end
end

def signed_in_root_path(resource_or_scope)

tries to find a resource_root_path, otherwise it uses the root_path.
The scope root url to be used when he's signed in. By default, it first
def signed_in_root_path(resource_or_scope)
  scope = Devise::Mapping.find_scope!(resource_or_scope)
  home_path = "#{scope}_root_path"
  if respond_to?(home_path, true)
    send(home_path)
  elsif respond_to?(:root_path)
    root_path
  else
    "/"
  end
end

def stored_location_for(resource_or_scope)


redirect_to stored_location_for(:user) || root_path

Example:

the given scope. Useful for giving redirect backs after sign up:
Returns and delete (if it's navigational format) the url stored in the session for
def stored_location_for(resource_or_scope)
  scope = Devise::Mapping.find_scope!(resource_or_scope)
  if is_navigational_format?
    session.delete("#{scope}_return_to")
  else
    session["#{scope}_return_to"]
  end
end

def warden

The main accessor for the warden proxy instance
def warden
  request.env['warden']
end