Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle unauthorized #17

Open
sprite2005 opened this issue May 26, 2022 · 1 comment
Open

Handle unauthorized #17

sprite2005 opened this issue May 26, 2022 · 1 comment

Comments

@sprite2005
Copy link

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.

@sprite2005
Copy link
Author

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant