如何在Doorkeeper :: TokenController上使用before_action [英] How to use before_action on Doorkeeper::TokenController

查看:103
本文介绍了如何在Doorkeeper :: TokenController上使用before_action的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having trouble with Doorkeeper::TokensController.
I want to execute some code before an Access Token is asked for (if it's created or not, I want to log it anyway) using a before_action (default route is POST /oauth/token / Doorkeeper::TokensController#create.

我按照操作此处进行操作以下:

I followed the doc here by doing the following:

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时,出现以下错误消息:

But when I do POST /oauth/token, I have the following error message:

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:在顶部(必填)"中

ActionController::RoutingError (undefined method 'before_action' for Oauth::AccessTokensController:Class):
app/controllers/oauth/access_tokens_controller.rb:2:in 'class:AccessTokensController'
app/controllers/oauth/access_tokens_controller.rb:1:in 'top (required)'

我做错了什么?有没有办法触发before_actionDoorkeeper::TokensController上的等价物?

What am I doing wrong? Is there a way to trigger a before_action or equivalent on Doorkeeper::TokensController?

推荐答案

我找到了答案,将其发布在这里,以防万一有人需要它:

I found the answer, posting it here just in case someone needs it:

1-门卫
首先,Doorkeeper建立在ActionController::Metal上(请参阅

1 - Doorkeeper
First of all, Doorkeeper is built on ActionController::Metal (see here). It means that it doesn't come with all the features that you can use in a "classical" controller inheriting from ActionController::Base

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

2 - Adding features
In order to add some features to my AccessTokensController I had to include AbstractController::Callbacks like this:

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

(感谢答案)

这篇关于如何在Doorkeeper :: TokenController上使用before_action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆