Last active
December 16, 2020 09:15
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# 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