我无法将我的新中间件放置在机架堆栈中我想放置的位置

问题描述

我编写了一个名为 RescueAuthTokenFromOmniauth 的新中间件,它可以挽救由 OmniAuth::Strategies::GoogleOauth2OmniAuth::Strategies::Facebook 创建的异常。理想情况下,我想将 RescueAuthTokenFromOmniauth 放在堆栈中的 Omniauth 中间件之前。

我可以使用

RescueAuthTokenFromOmniauth添加到堆栈中
# application.rb
config.middleware.use RescueAuthTokenFromOmniauth

但是当我尝试做,例如,

# application.rb
config.middleware.insert_before(OmniAuth::Strategies::GoogleOauth2,RescueAuthTokenFromOmniauth)

运行 rails middleware 时出现以下错误:

No such middleware to insert before: OmniAuth::Strategies::GoogleOauth2

Omniauth::Strategies 中间件由 gem omniauth-google-oauth2omniauth-facebook 添加。

有没有办法在 OmniAuth::Strategies::GoogleOauth2 中引用 application.rb 以便我可以将我的新中间件放在它之前?

我使用的是 Rails (6.0.3.6)。

这是运行 rails 中间件的结果。它已经通过使用 RescueAuthTokenFromOmniauth

包含了 config.middleware.use RescueAuthTokenFromOmniauth
use Webpacker::DevServerProxy
use ActionDispatch::HostAuthorization
use Rack::Sendfile
use ActionDispatch::Static
use ActionDispatch::Executor
use Rack::Rewrite
use ActiveSupport::Cache::Strategy::LocalCache::Middleware
use Rack::Runtime
use Rack::MethodOverride
use ActionDispatch::RequestId
use RequestStore::Middleware
use ActionDispatch::RemoteIp
use Sprockets::Rails::QuietAssets
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use WebConsole::Middleware
use ActionDispatch::DebugExceptions
use BetterErrors::Middleware
use Rollbar::Middleware::Rails::RollbarMiddleware
use ActionDispatch::ActionableExceptions
use ActionDispatch::Reloader
use ActionDispatch::Callbacks
use ActiveRecord::Migration::CheckPending
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use ActionDispatch::ContentSecurityPolicy::Middleware
use Rack::Head
use Rack::ConditionalGet
use Rack::ETag
use Rack::TempfileReaper
use Warden::Manager
use RescueAuthTokenFromOmniauth
use Rack::Attack
use Bullet::Rack
use OmniAuth::Strategies::GoogleOauth2
use OmniAuth::Strategies::Facebook
run SaverLife::Application.routes

这些是application.rb中关于中间件的所有设置

require_relative '../lib/rescue_auth_token_from_omniauth'
...
config.middleware.use RescueAuthTokenFromOmniauth

我在 lib/rescue_auth_token_from_omniauth.rb 中定义的新中间件

class RescueAuthTokenFromOmniauth
  def initialize(app)
    @app = app
  end

  def call(env)
    begin
      @app.call env
    rescue ActionController::InvalidAuthenticityToken
      [302,{'Location' => "/users/sign_up",'Content-Type' => 'text/html'},['Invalid Authenticity Token']]
    end
  end
end

我在 devise.rb 中添加了 OmniAuth 中间件

  # ==> OmniAuth
  # Add a new OmniAuth provider. Check the wiki for more information on setting
  # up on your models and hooks.
  # config.omniauth :github,'APP_ID','APP_SECRET',scope: 'user,public_repo'
  config.omniauth :google_oauth2,Rails.application.credentials.dig(Rails.env.to_sym,:google_client_id),:google_client_secret)

  config.omniauth :facebook,:facebook_key),:facebook_secret),scope: "email",info_fields: 'name,email,first_name,last_name'

这就是我的 OmniAuth 初始化程序的样子

# omniauth.rb
OmniAuth.config.logger = Rails.logger

OmniAuth.config.allowed_request_methods = [:post]

注意:我现在知道 RescueAuthTokenFromOmniauthOmniauth::Strategies 中间件之前,所以我的功能有效。但我想用 insert_before 显式设置它,以便很明显它应该放在 Omniauth::Strategies 中间件之前。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)