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 - 有没有一种简单的方法可以让 Mechanize 获取网页的所有组件?

ruby-on-rails-4 - Best_in_place 集合 1 对多关联

sql - 缺少 FROM 子句,Update_all posts,其中 comment_ids

ruby-on-rails - Heroku - bundler : failed to load command: unicorn

ruby - 在 Ruby on Rails 应用程序中实现匹配算法

session - 如何导出Plone session 配置?

php - cookie 在 HTTPS 连接中是否安全?

javascript - 调试 JavaScript cookie

ruby-on-rails - has_many 至少有两个条目

javascript - 使用 ruby​​ on Rails 页面正确设置 CoffeeScript