4

I made a basic chat with ActionCable authenticated 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.email
    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

But when the user has an open chat and it rejects the connection (because the users have logged out), I need to show a login screen.

The problem is that on the frontend I can't get the reason for the disconnection.

How can I send reject with params, like "unauthorized"?

hhh_
  • 310
  • 2
  • 5
  • 17
Spike886
  • 609
  • 8
  • 23

1 Answers1

0
def find_verified_user # this checks whether a user is authenticated with devise
  if verified_user = env['warden'].user
    verified_user
  else
    message = "The user is not found. Connection rejected."

    logger.add_tags 'ActionCable', message # to console

    self.transmit error: message # this is what you wanted

    reject_unauthorized_connection
  end
end

See also: How to terminate subscription to an actioncable channel from server?

Community
  • 1
  • 1
prograils
  • 2,248
  • 1
  • 28
  • 45