N
N
Nedim Suinov2017-11-27 14:36:27
JavaScript
Nedim Suinov, 2017-11-27 14:36:27

How to pass JWT to socket.io?

Hello, I decided to use socket.io. I have an array of users. For which a token is generated during authentication. How can a socket pass a token when the 'connection' event fires? To know which user socket.id is bound to? I used to do this:

// СЕРВЕР
io.on('connection', function (socket) {
  socket.on('news', data => {
    data.socketid = socket.id;   
// КЛИЕНТ
 ngOnInit() {
    this.socket.emit('news', this.auth);
}

but it's kapets not right, and not convenient.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dasha Tsiklauri, 2017-11-27
@dasha_programmist

for websocket, for example, when requesting the ws://host route (request to establish a connection / handshake)
1) pass in the Authorization: Bearer header (if according to the oauth standard)
2) issue cookies during authorization and then they themselves will be thrown when the connection is established
here is an example of how to pass headers
or resolve at the business logic level:
1) store the connection-id -> user map and reverse
2) implement the login / logout method
3) in other methods, check for authorization status when trying to access them

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question