namiwang / actioncable_dart

actioncable client in dart, for pure dart or flutter project

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handle unauthorized

sprite2005 opened this issue · comments

I only allow authorized users to connect ActionCable, if the user is unauthorized I call reject_unauthorized_connection from Rails.

This sends a response

{
"type": "disconnect",
"reason": "unauthorized",
"reconnect": false
}

This package will crash on line 127 in action_cable.dart:

final channelId = parseChannelId(payload['identifier']);

Because there is no identifier as we are not yet connected to a channel and a channel disconnect callback wouldn't be appropriate anyways since it's disconnecting the client.

We need to check if reason is 'unauthorized' and then call either onConnectionLost or onCannotConnect. I think onCannotConnect would be most appropriate, perhaps even passing the reconnect: false so we know not to retry the connection.

Let me know if you want to make this change or if I should just fork.

Something as simple as this should work if you don't want to break current API, although it would be nice if we could pass retry, since I'm sure a lot of people have automatic connection retry handling on cannot connect.

      case 'disconnect':
        final identifier = payload['identifier'];
        if (identifier != null) {
          final channelId = parseChannelId(payload['identifier']);
          final onDisconnected = _onChannelDisconnectedCallbacks[channelId];
          if (onDisconnected != null) {
            onDisconnected();
          }
        } else {
          final reason = payload['reason'];
          if (reason != null && reason == 'unauthorized') {
            if (this.onCannotConnect != null) this.onCannotConnect!();
          }
        }
        break;