anycable / anycable-rails

AnyCable for Ruby on Rails applications
https://anycable.io
MIT License
500 stars 35 forks source link

Devise authentication/sessions not working #181

Closed quadlol closed 1 year ago

quadlol commented 1 year ago

Hi, it seems that cookies/session doesn't work with anycable.

Before I had added anycable, I authenticated like this

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user 
    def connect
      self.current_user = find_user
    end

    def find_user
      if (current_user = env["warden"].user)
        current_user
      else
        reject_unauthorized_connection
      end
    rescue UncaughtThrowError
      nil
    end

This always worked. Now I installed anycable and cookies/sessions are empty.

[13] pry(#<ApplicationCable::Connection>)> cookies
=> #<ActionDispatch::Cookies::CookieJar:0x000000010dc0c8e0 @committed=false, @cookies={}, @delete_cookies={}, @request=#<ActionDispatch::Request GET "http://localhost/cable" for 127.0.0.1>, @set_cookies={}>

I have tried almost everything

env["warden"].user(:user)
AnyCable::Rails::Rack.middleware.use Warden::Manager do |config|
  Devise.warden_config = config
end

# frozen_string_literal: true

Warden::Manager.after_set_user do |user,auth,opts|
  scope = opts[:scope]
  auth.cookies.signed["#{scope}_id"] = user.id
end

Warden::Manager.before_logout do |user, auth, opts|
  scope = opts[:scope]
  auth.cookies.signed["#{scope}_id"] = nil
end

All of them are empty, it doesn't matter what I do. I am using the default Rails session_store and caching is off.

Why am I not able to access the current session in connection?

Tell us about your environment

Ruby version: 3.1

Rails version: 6.1

anycable gem version: 1.3

anycable-rails gem version: 1.3.7

grpc gem version: 1.52

RedShore117 commented 10 months ago

Hi, it seems that cookies/session doesn't work with anycable.

Hello i have same problem, can u tell me how u figure it out? thanks!