问题
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
But when I do POST /oauth/token
, I have the following error message:
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)'
What am I doing wrong? Is there a way to trigger a before_action
or equivalent on Doorkeeper::TokensController
?
回答1:
I found the answer, posting it here just in case someone needs it:
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 - 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
(thanks to this answer)
来源:https://stackoverflow.com/questions/42138227/how-to-use-before-action-on-doorkeepertokencontroller