Module: Devise::Controllers::Helpers

Extended by:
ActiveSupport::Concern
Includes:
SignInOut, StoreLocation
Defined in:
lib/devise/controllers/helpers.rb

Overview

Those helpers are convenience methods added to ApplicationController.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StoreLocation

#store_location_for, #stored_location_for

Methods included from SignInOut

#bypass_sign_in, #sign_in, #sign_out, #sign_out_all_scopes, #signed_in?

Class Method Details

.define_helpers(mapping) ⇒ Object

Define authentication filters and accessor helpers based on mappings. These filters should be used inside the controllers as before_actions, so you can control the scope of the user who should be signed in to access that specific controller/action. Example:

Roles:
  User
  Admin

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

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


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/devise/controllers/helpers.rb', line 110

def self.define_helpers(mapping) #:nodoc:
  mapping = mapping.name

  class_eval "def authenticate_\#{mapping}!(opts={})\nopts[:scope] = :\#{mapping}\nwarden.authenticate!(opts) if !devise_controller? || opts.delete(:force)\nend\n\ndef \#{mapping}_signed_in?\n!!current_\#{mapping}\nend\n\ndef current_\#{mapping}\n@current_\#{mapping} ||= warden.authenticate(scope: :\#{mapping})\nend\n\ndef \#{mapping}_session\ncurrent_\#{mapping} && warden.session(:\#{mapping})\nend\n", __FILE__, __LINE__ + 1

  ActiveSupport.on_load(:action_controller) do
    if respond_to?(:helper_method)
      helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session"
    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 valid resource_return_to key in the session, then it fallbacks to resource_root_path, otherwise it uses the root path. For a user scope, you can define the default url in the following way:

get '/users' => 'users#index', as: :user_root # creates user_root_path

namespace :user do
  root 'users#index' # 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 (resource)
  stored_location_for(resource) ||
    if resource.is_a?(User) && resource.can_publish?
      publisher_url
    else
      super
    end
end


213
214
215
# File 'lib/devise/controllers/helpers.rb', line 213

def (resource_or_scope)
  stored_location_for(resource_or_scope) || signed_in_root_path(resource_or_scope)
end

#after_sign_out_path_for(resource_or_scope) ⇒ Object

Method used by sessions controller to sign out a 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 it is the root_path.



223
224
225
226
227
228
# File 'lib/devise/controllers/helpers.rb', line 223

def after_sign_out_path_for(resource_or_scope)
  scope = Devise::Mapping.find_scope!(resource_or_scope)
  router_name = Devise.mappings[scope].router_name
  context = router_name ? send(router_name) : self
  context.respond_to?(:root_path) ? context.root_path : "/"
end

#allow_params_authentication!Object

Tell warden that params authentication is allowed for that specific page.



161
162
163
# File 'lib/devise/controllers/helpers.rb', line 161

def allow_params_authentication!
  request.env["devise.allow_params_authentication"] = true
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 controllers, except the ones in devise:

before_action :my_filter, unless: :devise_controller?


149
150
151
# File 'lib/devise/controllers/helpers.rb', line 149

def devise_controller?
  is_a?(::DeviseController)
end

#devise_parameter_sanitizerObject

Set up a param sanitizer to filter parameters using strong_parameters. See lib/devise/parameter_sanitizer.rb for more info. Override this method in your application controller to use your own parameter sanitizer.



156
157
158
# File 'lib/devise/controllers/helpers.rb', line 156

def devise_parameter_sanitizer
  @devise_parameter_sanitizer ||= Devise::ParameterSanitizer.new(resource_class, resource_name, params)
end

#handle_unverified_requestObject

Overwrite Rails' handle unverified request to sign out all scopes, clear run strategies and remove cached variables.



252
253
254
255
256
# File 'lib/devise/controllers/helpers.rb', line 252

def handle_unverified_request
  super # call the default behaviour which resets/nullifies/raises
  request.env["devise.skip_storage"] = true
  sign_out_all_scopes(false)
end

#is_flashing_format?Boolean

Check if flash messages should be emitted. Default is to do it on navigational formats



268
269
270
# File 'lib/devise/controllers/helpers.rb', line 268

def is_flashing_format?
  is_navigational_format?
end

#is_navigational_format?Boolean



262
263
264
# File 'lib/devise/controllers/helpers.rb', line 262

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

#request_formatObject



258
259
260
# File 'lib/devise/controllers/helpers.rb', line 258

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

#sign_in_and_redirect(resource_or_scope, *args) ⇒ Object

Sign in a user and tries to redirect first to the stored location and then to the url specified by after_sign_in_path_for. It accepts the same parameters as the sign_in method.



233
234
235
236
237
238
239
# File 'lib/devise/controllers/helpers.rb', line 233

def (resource_or_scope, *args)
  options  = args.extract_options!
  scope    = Devise::Mapping.find_scope!(resource_or_scope)
  resource = args.last || resource_or_scope
  (scope, resource, options)
  redirect_to (resource)
end

#sign_out_and_redirect(resource_or_scope) ⇒ Object

Sign out a user and tries to redirect to the url specified by after_sign_out_path_for.



243
244
245
246
247
248
# File 'lib/devise/controllers/helpers.rb', line 243

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

#signed_in_root_path(resource_or_scope) ⇒ Object

The scope root url to be used when they're signed in. By default, it first tries to find a resource_root_path, otherwise it uses the root_path.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/devise/controllers/helpers.rb', line 167

def signed_in_root_path(resource_or_scope)
  scope = Devise::Mapping.find_scope!(resource_or_scope)
  router_name = Devise.mappings[scope].router_name

  home_path = "#{scope}_root_path"

  context = router_name ? send(router_name) : self

  if context.respond_to?(home_path, true)
    context.send(home_path)
  elsif context.respond_to?(:root_path)
    context.root_path
  elsif respond_to?(:root_path)
    root_path
  else
    "/"
  end
end

#wardenObject

The main accessor for the warden proxy instance



140
141
142
# File 'lib/devise/controllers/helpers.rb', line 140

def warden
  request.env['warden'] or raise MissingWarden
end