gpt4 book ai didi

ruby-on-rails - 如何在 Doorkeeper::TokenController 上使用 before_action

转载 作者:行者123 更新时间:2023-12-02 01:52:48 24 4
gpt4 key购买 nike

我在使用 Doorkeeper::TokensController 时遇到问题。
我想在请求访问 token 之前使用 before_action (默认路由是 POST/oauth/token/Doorkeeper::TokensController#create

我按照文档 here 进行操作通过执行以下操作:

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

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)'

我做错了什么?有没有办法在 Doorkeeper::TokensController 上触发 before_action 或等效操作?

最佳答案

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

1 - 门卫
首先,Doorkeeper 构建于 ActionController::Metal 之上(请参阅 here )。这意味着它不具备您可以在继承自 ActionController::Base 的“经典” Controller 中使用的所有功能

2 - 添加功能
为了向我的 AccessTokensController 添加一些功能,我必须包含 AbstractController::Callbacks ,如下所示:

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

(感谢this回答)

关于ruby-on-rails - 如何在 Doorkeeper::TokenController 上使用 before_action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42138227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com