【问题标题】:How to skip before filter in Grape Rails?如何在 Grape Rails 中的过滤器之前跳过?
【发布时间】:2022-10-23 15:53:12
【问题描述】:

我正在使用 grape_rails gem 来管理 API,在我的端点上我有以下内容:

..api/v4/endpoints/base.rb

class Endpoints::V4::Base < Endpoints::Base
  before { authenticate_with_token! }

  version 'v4', using: :path

  mount Endpoints::V4::Menu
  mount Endpoints::V4::Customer
  mount Endpoints::V4::Orders   
end

我想跳过使用令牌进行身份验证!方法端点::V4::菜单在我的 base.rb 文件中,但它对我不起作用。

我已经尝试过:

class Endpoints::V4::Base < Endpoints::Base
  skip_before { authenticate_with_token! only: :customer  } # first test 
  before { authenticate_with_token! except: :customer } # second test

  ...

  def customer
    mount Endpoints::V4::Products
  end
end

谢谢你的时间

【问题讨论】:

    标签: ruby-on-rails ruby ruby-grape


    【解决方案1】:

    您可以从您的身份验证方法中提前返回,仅在某些条件为真时进行身份验证。

    class Endpoints::V4::Base < Endpoints::Base
      before { authenticate_with_token! } 
    
      ...
    
      def authenticate_with_token!
        return if <condition>
    
        ....
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多