Class: Devise::FailureApp

Inherits:
Object
  • Object
show all
Includes:
Warden::Mixins::Common
Defined in:
lib/devise/failure_app.rb

Overview

Failure application that will be called every time :warden is thrown from any strategy or hook. Responsible for redirect the user to the sign in page based on current scope and mapping. If no scope is given, redirect to the default_url.

Constant Summary collapse

@@default_message =
:unauthenticated

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ FailureApp

Returns a new instance of FailureApp.



17
18
19
# File 'lib/devise/failure_app.rb', line 17

def initialize(env)
  @env = env
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



7
8
9
# File 'lib/devise/failure_app.rb', line 7

def env
  @env
end

Class Method Details

.call(env) ⇒ Object



13
14
15
# File 'lib/devise/failure_app.rb', line 13

def self.call(env)
  new(env).respond!
end

Instance Method Details

#query_string_for(options) ⇒ Object

Build the proper query string based on the given message.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/devise/failure_app.rb', line 38

def query_string_for(options)
  message = @env['warden'].try(:message) || options[:message] || default_message

  params = case message
    when Symbol
      { message => true }
    when String
      { :message => message }
    else
      {}
  end

  Rack::Utils.build_query(params)
end

#redirect_path_for(scope) ⇒ Object

Build the path based on current scope.



54
55
56
57
58
59
60
# File 'lib/devise/failure_app.rb', line 54

def redirect_path_for(scope)
  if mapping = Devise.mappings[scope]
    "#{mapping.parsed_path}/#{mapping.path_names[:sign_in]}"
  else
    "/#{default_url}"
  end
end

#respond!Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/devise/failure_app.rb', line 21

def respond!
  options = @env['warden.options']
  scope   = options[:scope]

  redirect_path = redirect_path_for(scope)
  query_string  = query_string_for(options)
  store_location!(scope)

  headers = {}
  headers["Location"] = redirect_path
  headers["Location"] << "?" << query_string unless query_string.empty?
  headers["Content-Type"] = 'text/plain'

  [302, headers, ["You are being redirected to #{redirect_path}"]]
end

#store_location!(scope) ⇒ Object

Stores requested uri to redirect the user after signing in. We cannot use scoped session provided by warden here, since the user is not authenticated yet, but we still need to store the uri based on scope, so different scopes would never use the same uri to redirect.



66
67
68
69
70
# File 'lib/devise/failure_app.rb', line 66

def store_location!(scope)
  if request && request.get? && !request.xhr?
    session[:"#{scope}.return_to"] = request.request_uri
  end
end