Rails Devise Action Cable

前端 未结 2 1408
再見小時候
再見小時候 2021-02-20 08:01

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

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

    identified_by :current_us         


        
2条回答
  •  耶瑟儿~
    2021-02-20 08:24

    Try setting the cookie in a warden callback.

    Add a file to `config/initializers/your_file.rb``

    Add this to the file:

    Warden::Manager.after_set_user do |user, auth, opts|
      scope = opts[:scope]
      auth.cookies.signed["#{scope}.id"] = user.id
      auth.cookies.signed["#{scope}.expires_at"] = 60.minutes.from_now
    end
    
    Warden::Manager.before_logout do |user, auth, opts|
      scope = opts[:scope]
      auth.cookies.signed["#{scope}.id"] = nil
      auth.cookies.signed["#{scope}.expires_at"] = nil
    end
    

    Or you could do something like this:

    verified_user = env['warden'].user
    

    As explained in this very nice tuorial: https://www.sitepoint.com/create-a-chat-app-with-rails-5-actioncable-and-devise/

提交回复
热议问题