gpt4 book ai didi

ruby-on-rails - env ['warden' ] 不适用于 Rails 5

转载 作者:行者123 更新时间:2023-12-03 16:10:13 25 4
gpt4 key购买 nike

我按照本指南使用 Websockets 创建聊天功能。
https://www.sitepoint.com/rails-and-actioncable-adding-advanced-features/

我遇到了 env['warden'].user 的问题即使我使用标准设计表单登录到应用程序,也没有重新调整任何内容。

如果我使用另一种方法(现在已评论) - 它会返回错误的用户

module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user

def connect
self.current_user = find_verified_user
logger.add_tags 'ActionCable', current_user.email
end

protected

def find_verified_user # this checks whether a user is authenticated with devise
verified_user = env['warden'].user

if verified_user
verified_user
else
reject_unauthorized_connection
end
end

# def find_verified_user
# user_id = request.headers['HTTP_AUTHORIZATION']
# if verified_user = User.find_by(user_id)
# verified_user
# else
# reject_unauthorized_connection
# end
# end

end
end

日志 说
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2017-04-06 17:40:17 +0300
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
An unauthorized connection attempt was rejected
Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)

最佳答案

我遇到了同样的问题,但结果证明我的问题是因为我有两个设计模型:UserCustomer ,由于用户用于管理内容,因此客户被设置为管理员的 default_scope .所以要访问user范围,我必须执行以下操作

env['warden'].user(:user)

最后的符号定义了要使用的范围。

我在这里找到了有关 Warden 范围用户的信息: https://github.com/wardencommunity/warden/wiki/Scopes#scoped-user-access

关于ruby-on-rails - env ['warden' ] 不适用于 Rails 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43258458/

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