简体   繁体   中英

How to use before_action on Doorkeeper::TokenController

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 ?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM