wardencommunity/warden

Every 'failure_app' is called

BorisBresciani opened this issue · 2 comments

Hello,

I do not understand why when I do:

render json: {data: 'Error page'}, status: :unauthorized

Is my failure app called? I can not render a render json: data, status: :unauthorized without call failure_app ?

My conf:

    config.middleware.use Warden::Manager do |manager|
      manager.default_scope = :user
      manager.default_strategies :authentication_token
      manager.failure_app = ::UnauthorizedController
    end

UnauthorizedController:

class UnauthorizedController < ActionController::Metal
  def self.call(env)
    @respond ||= action(:respond)
    @respond.call(env)
  end

  def respond
    self.response_body = _data.to_json
    self.content_type = 'application/json'
    self.status = :unauthorized
  end

  private
    def _message
      request.env['warden'].message
    end

    def _data
      ::ErrorTypeSerializer.serialize(_message.present? ? _message : 'INVALID_HEADER')
    end
end

Thx !

I am not 100% familiar with the current ActionController::Metal API, but what your looking to return from a Rack is:

class UnauthorizedController
  def call(env)
    [401, {"Content-Type" => "text/html"}, ["Unauthorized"]]
  end
end

If you map this to ActionController::Metal the failure app should work as intended.

Closing. Let us know if that did not solve your issue.