Rails设计动作电缆 [英] Rails Devise Action Cable

查看:67
本文介绍了Rails设计动作电缆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让Action Cable与Devise一起使用。

I'm trying to get Action Cable working with Devise.

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.name
    end

    protected

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

如果用户登录,我仍然会从 cookies.signed中获得 nil ['user.id']

If an user is logged in I still get nil from cookies.signed['user.id']

推荐答案

更新您的 connection.rb 以及以下内容:

Update your connection.rb with the following:

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.studentid
    end

    protected

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

链接:
http://tutorials.pluralsight.com/ruby-ruby-on-rails/implementing-a-custom-devise -sign-in-and-actioncable-rails-5?saved = 1& status =正在审核

这篇关于Rails设计动作电缆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆