How to use before_action on Doorkeeper::TokenController

不羁的心 提交于 2019-12-22 10:56:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!