this.io
.use(async (socket: Socket, next: (err?: ExtendedError) => void): Promise<void> => {
try {
const connection: typeorm.Connection = await this.connection()
const authToken: string = socket.handshake.auth.accessToken
if (authToken) {
const verified: string | jwt.JwtPayload = JWT.verifyToken(authToken)
const transformToken: string = await decrypt(verified as any, 20)
const decodedToken: string | jwt.JwtPayload = await jwt.decode(transformToken)
const checkToken: Secrets = await connection
.getRepository(Secrets)
.findOne({ resourceBy: decodedToken['id'], resourceType: 'login', accessToken: verified as any })
if (checkToken) next()
}
} catch (e: any) {
return Promise.reject(`Authentication socket error: ${e}`)
}
})
.on('connection', (socket: Socket): void => {
logsFormater('socket.io client connected', 'info')
new SocketWebhooks(socket).handler()
})
req.session.regenerate...
res.send({rediskey: req.sessionID});
//store the key in a cookie
SetCookie('rediskey', <%= rediskey %>); //http://msdn.microsoft.com/en-us/library/ms533693(v=vs.85).aspx
//then when socket is connected, fetch the rediskey from the document.cookie and send it back to server
var socket = new io.Socket();
socket.on('connect', function() {
var rediskey = GetCookie('rediskey'); //http://msdn.microsoft.com/en-us/library/ms533693(v=vs.85).aspx
socket.send({rediskey: rediskey});
});
//in io.on('connection')
io.on('connection', function(client) {
client.on('message', function(message) {
if(message.rediskey) {
//fetch session info from redis
redisclient.get(message.rediskey, function(e, c) {
client.user_logged_in = c.username;
});
}
});
});