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.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/devise/failure_app.rb', line 42

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

#respond!Object



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

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

  redirect_path = if mapping = Devise.mappings[scope]
    "#{mapping.parsed_path}/#{mapping.path_names[:sign_in]}"
  else
    "/#{default_url}"
  end
  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.



61
62
63
# File 'lib/devise/failure_app.rb', line 61

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