Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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()
      })
 
PREVIOUS NEXT
Tagged: #auth
ADD COMMENT
Topic
Name
4+1 =