gpt4 book ai didi

ruby - application 和 actioncable 不会共享 cookie

转载 作者:数据小太阳 更新时间:2023-10-29 06:53:47 24 4
gpt4 key购买 nike

我正在使用 devise 进行身份验证,但是当我实现您的方法时,我得到“未经授权的连接尝试被拒绝”

经过几个小时的搜索,我发现:

cookies.signed['user.id']

返回零。在以下代码块中。

def find_verified_user
if verified_user = User.find_by(id: cookies.signed['user.id'])
verified_user
else
reject_unauthorized_connection
end
end

我查了一下,确实有cookie,但是里面没有Devise设置的cookie数据。

为了检查“user.id”是否真的设置了,我在 View 中提出了它。作为异常(exception),这将返回用户 ID

Signed in as @#{cookies.signed[:username]}.
- raise(cookies.signed['user.id'].inspect)
%br/
%br/
#messages
%br/
%br/
= form_for :message, url: messages_path, remote: true, id: 'messages-form' do |f|
= f.label :body, 'Enter a message:'
%br/
= f.text_field :body
%br/
= f.submit 'Send message'

我的问题:

似乎 cookie 在 actioncable 服务器上不可用。
有没有办法与有线服务器共享 Devise 设置的 cookie?

https://github.com/stsc3000/actioncable-chat.git

最佳答案

检查连接到 Action Cable 服务器的客户端 JavaScript 文件。一些教程让你把它放在 'app/assets/javascripts/application_cable.coffee' 和其他的 'app/assets/javascripts/channels/index.coffee' 但它看起来像这样:

#= require cable

@App = {}
App.cable = Cable.createConsumer("ws://cable.example.com:28080")

您需要 WebSocket 地址指向您的 Cable 服务器,并且该地址需要与您的应用程序的其余部分共享一个 cookie 命名空间。很可能你的指向错误的地方,所以例如,如果你在本地处理这个,你需要改变:

App.cable = Cable.createConsumer("ws://cable.example.com:28080")

App.cable = Cable.createConsumer("ws://localhost:28080")

当然假设您的 Cable 服务器正在端口 28080 上运行(在 bin/cable 可执行文件中指定)。还要确保清除浏览器缓存,以便更新后的文件是浏览器正在使用的文件。

关于ruby - application 和 actioncable 不会共享 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34453493/

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