问题描述
我在使用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:
我做错了什么?有没有办法触发before_action
或Doorkeeper::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
上(请参阅此处).这意味着它不具备可以从ActionController::Base
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!