Skip to content

Forcing a client to disconnect when authtoken is null or invalid. #448

@d-m-cc

Description

@d-m-cc

Hi @jondubois and the SocketCluster crew!
I've been experimenting with SocketCluster to get a feel for how to work with the framework. So far, everything has come naturally but I do have a question:

I'd like to force a client to disconnect when/if the auth token is not specified (null) or invalid. I'm wondering where the best place (best practice) to do this is given that there are multiple events to hook into where I could conceivably do this:

For example, I could do the following, but is this recommended?

scServer.on('badSocketAuthToken', function (socket, tokenData) {
  socket.disconnect(4004, 'Invalid or missing auth token provided');
});

Alternatively, I could do the following:

scServer.on('connection', function (socket, status) {
  if (!status.isAuthenticated) {
    socket.disconnect(4004, 'Invalid or missing auth token provided');
  }
});

Or, alternatively, on the socket.on('connect') event.
Additionally, I'm guessing I could do this in middleware such as in MIDDLEWARE_AUTHENTICATE? (doubtful because I believe MIDDLEWARE_AUTHENTICATE only runs on successful auth token?)

Ultimately, I'm trying to achieve kicking off a client that does not have a valid auth token, as right now it appears the client stays connected to the server (just unauthenticated). Is this correct?
Suggestions around best practices about doing this are much appreciated.
Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions