Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

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
Typescript :: c# to typescript 
Typescript :: What kind of projects is suitable for the Agile methodology 
Typescript :: tag for bullets in html 
Typescript :: ERROR Error: mat-form-field must contain a MatFormFieldControl. 
Typescript :: typescript export interface array 
Typescript :: how to pring events in pygame 
Typescript :: grid implementation html canvas 
Typescript :: typescript cast string to number 
Typescript :: world-times-newspaper-magazine-style-ghost-blog-theme 
Typescript :: how to show code conflicts in git 
Typescript :: share data across tab through localstorage 
Typescript :: typescript document.getelementbyid object is possibly null 
Typescript :: typescript wrapping for array 
Typescript :: Roblox Script wait 
Typescript :: check if all array elements match closure swift 
Typescript :: uTorrent Default Download Folder - Linux 
Typescript :: Let say your Project Manager tell you that your database requests are consume a lot of memory, you need to do something to improve the performance. How would you do it in hibernate ? 
Typescript :: feature counts bioconda 
Typescript :: how to convert an array of other types in java 8 
Typescript :: network analysis projects code python graph and histogram with data facbook 
Typescript :: typescript encode url 
Typescript :: benefits of waxing body hair 
Typescript :: response 404 requests python compare 
Typescript :: haproxy stats config 
Typescript :: how to write elements of a list as a string with a comma between elements in python 
Typescript :: How to load plugin scripts in roblox studio command 
Typescript :: css animation for beginners 
Typescript :: how to check weather a file exists using os module 
Typescript :: Jane and the Frost Giants "c++" 
Typescript :: not able to access string in template angular 8 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =