【问题标题】:How to use before_action on Doorkeeper::TokenController如何在 Doorkeeper::TokenController 上使用 before_action
【发布时间】:2017-06-27 13:16:06
【问题描述】:

我在使用 Doorkeeper::TokensController 时遇到问题。
我想使用before_action(默认路由是POST /oauth/token/Doorkeeper::TokensController#create)在请求访问令牌之前执行一些代码(无论是否创建,我都想记录它)。

我按照文档here 执行了以下操作:

config/routes.rb

  use_doorkeeper do
    controllers tokens: 'oauth/access_tokens'
  end

app/controllers/access_tokens_controller.rb

class Oauth::AccessTokensController < Doorkeeper::TokensController
  before_action :log_auth, only: [:create]

  def log_auth
    puts "I want to log here"
  end
end

但是当我执行POST /oauth/token 时,我收到以下错误消息:

ActionController::RoutingError(Oauth::AccessTokensController:Class 的未定义方法“before_action”):
app/controllers/oauth/access_tokens_controller.rb:2:in 'class:AccessTokensController'
app/controllers/oauth/access_tokens_controller.rb:1:in 'top (required)'

我做错了什么?有没有办法在Doorkeeper::TokensController 上触发before_action 或等效项?

【问题讨论】:

    标签: ruby-on-rails oauth doorkeeper


    【解决方案1】:

    我找到了答案,把它贴在这里以防万一有人需要它:

    1 - 看门人
    首先,Doorkeeper 建立在ActionController::Metal 之上(参见here)。这意味着它没有提供您可以在继承自 ActionController::Base 的“经典”控制器中使用的所有功能

    2 - 添加功能
    为了给我的AccessTokensController 添加一些功能,我必须像这样包含AbstractController::Callbacks

    class Oauth::AccessTokensController < Doorkeeper::TokensController
      include AbstractController::Callbacks
      before_action :log_auth, only: [:create]
    
      def log_auth
        puts "I want to log here"
      end
    end
    

    (感谢this的回答)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-08
      • 2013-01-08
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多