Skip to content

Instantly share code, notes, and snippets.

@dznz
Last active December 16, 2020 09:15
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dznz/8552522 to your computer and use it in GitHub Desktop.
Save dznz/8552522 to your computer and use it in GitHub Desktop.
Two different ways to plug in a simple Warden failure app to a Rails project.
##
# Simple Warden configuration, complex "controller"
# In config/initializers/warden.rb
Rails.application.config.middleware.use Warden::Manager do |manager|
manager.failure_app = UnauthorizedController
end
# app/controllers/unauthorized_controller.rb
class UnauthorizedController < ActionController::Metal
include ActionController::Rendering
def self.call(env)
@respond ||= action(:respond)
@respond.call(env)
end
def respond
render nothing: true, status: :unauthorized
end
end
##
# Simple controller, complex Warden configuration
# config/initializers/warden.rb
Rails.application.config.middleware.use Warden::Manager do |manager|
manager.failure_app = ->(env){ UnauthorizedController.action(:index).call(env) }
end
# app/controllers/unauthorized_controller.rb
class UnauthorizedController < ApplicationController
def index
render nothing: true, status: :unauthorized
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment