gpt4 book ai didi

ruby-on-rails - 在 Doorkeeper 覆盖布局中访问 c​​urrent_user

转载 作者:行者123 更新时间:2023-12-01 03:55:29 26 4
gpt4 key购买 nike

由于 Doorkeeper 是一个孤立的引擎,我们无法访问您在 ApplicationController 中所做的任何事情.但是如果你需要一个 current_user 怎么办?这里有什么解决方法?

第一个想法是猴子补丁ActionController::Base .有什么更好的想法吗?

最佳答案

我的守门人实现在基本应用程序内部,因此如果您使用单独的引擎,这将无济于事,但如果您使用相同的 rails 应用程序,那么我将在这里分享:

class ApplicationController < ActionController::Base
protect_from_forgery

private

def current_user
if doorkeeper_token
return current_resource_owner
end
# fallback to auth with warden if no doorkeeper token
warden.authenticate(:scope => :user)
end

# Needed for doorkeeper find the user that owns the access token
def current_resource_owner
User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
end

end

关于ruby-on-rails - 在 Doorkeeper 覆盖布局中访问 c​​urrent_user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17640636/

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