ruby - application 和 actioncable 不会共享 cookie

标签 ruby ruby-on-rails-4 cookies devise actioncable

我正在使用 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/

相关文章:

ruby-on-rails - DataMapper 还能用于 Rails 吗?

java - HTTP Google 登录 - Cookie 已关闭

javascript - Node + Express + Nginx 未设置 Cookie

ruby-on-rails - scope为数组类型时,如何在url中正确写入参数?

ruby-on-rails - 我如何知道用户是否可以使用 Cancan 访问全部或部分资源?

ruby-on-rails - 使用 paperclip-av-transcoder 的回形针视频附件错误

internet-explorer - 没有 iframe 的 IE 8 和 9 中的跨域 cookie?

ruby - 如何在 IRB 中重新加载脚本?

Ruby 发送没有符号

ruby-on-rails - 发电机无法在 gem 中工作