Skip to content

How to Restrict Socket Connection to Authenticated Users #70

Discussion options

You must be logged in to vote

A sample code would be something along the lines of

io: {
  // ...
    config: {
    // ...
      hooks: {
        init({ $io }) {
          $io.server.use((socket,next) => {
            // check if socket connected with valid role.
            const isValidRole = .....;
            if(isValidRole){
              next();
            } else {
              next(new Error('Invalid Auth'));
            }
          });
        }
      }
    // ...  
    }
  // ...
}

This middleware will run after the initial handshake takes place. Hope that helps.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@Ujjwal091
Comment options

@ComfortablyCoding
Comment options

@ComfortablyCoding
Comment options

Answer selected by Ujjwal091
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants