Fixing token management

This commit is contained in:
David Négrier 2020-06-10 12:32:39 +02:00
parent a373626e24
commit 928e486de5
2 changed files with 5 additions and 2 deletions

View file

@ -60,13 +60,16 @@ export class IoSocketController {
// Completely commented for now, as we do not use the "/login" route at all.
this.Io.use((socket: Socket, next) => {
if (!socket.handshake.query || !socket.handshake.query.token) {
console.error('An authentication error happened, a user tried to connect without a token.');
return next(new Error('Authentication error'));
}
if(this.searchClientByToken(socket.handshake.query.token)){
console.error('An authentication error happened, a user tried to connect while its token is already connected.');
return next(new Error('Authentication error'));
}
Jwt.verify(socket.handshake.query.token, SECRET_KEY, (err: JsonWebTokenError, tokenDecoded: object) => {
if (err) {
console.error('An authentication error happened, invalid JsonWebToken.', err);
return next(new Error('Authentication error'));
}
@ -74,7 +77,7 @@ export class IoSocketController {
return next(new Error('Authentication error, invalid token structure'));
}
(socket as ExSocketInterface).token = tokenDecoded;
(socket as ExSocketInterface).token = socket.handshake.query.token;
(socket as ExSocketInterface).userId = tokenDecoded.userId;
next();
});

View file

@ -4,7 +4,7 @@ import {Identificable} from "./Identificable";
import {TokenInterface} from "../../Controller/AuthenticateController";
export interface ExSocketInterface extends Socket, Identificable {
token: TokenInterface;
token: string;
roomId: string;
webRtcRoomId: string;
userId: string;