Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

socket.io auth

    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()
      })
Comment

Socket.IO Authentication

req.session.regenerate...
res.send({rediskey: req.sessionID});
Comment

Socket.IO Authentication

//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});
});
Comment

Socket.IO Authentication

//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;
      });
    }

  });
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: Pass 3 of the same thing to ExpressJS with a form 
Javascript :: Special Chars like DOTS in Express.js route 
Javascript :: javascript polyIntersect 
Javascript :: filter data nodejs 
Javascript :: boilerplate functional component for DataGrid 
Javascript :: convert json to string curl 
Javascript :: Javascript array of array loop 
Javascript :: read excel file npm 
Javascript :: json array form to list object java 
Javascript :: “Line Splicing in C++” 
Javascript :: Uncaught Error: spawn node 
Javascript :: phaser reverse matrix rows 
Javascript :: inspect vuex store 
Javascript :: array loop pyramid js 
Javascript :: Object methods + Static methods javascript 
Javascript :: javascript download save files in folder 
Javascript :: React Native - iOS Release build crashing 
Javascript :: js how to get random number (inclusive min max) and push it in an array 
Javascript :: add array and sort 
Javascript :: true type of javascript 
Javascript :: prevent the Confirm Form Resubmission dialog | window.history.back() confirm form resubmission 
Javascript :: express dynamic api template 
Javascript :: Backbone Model And Collection 
Javascript :: In express redirect user to external url 
Javascript :: discord.js profile picture 
Javascript :: 2d array js 
Javascript :: clear console javascript 
Javascript :: array methods in javascript 
Javascript :: js delete url params 
Javascript :: remove backslash from json 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =