Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events.js:62 Uncaught Error: Uncaught, unspecified "error" event. (Authentication error) #111

Open
arn-the-long-beard opened this issue Aug 11, 2017 · 1 comment

Comments

@arn-the-long-beard
Copy link

arn-the-long-beard commented Aug 11, 2017

Hello.

I am using React Node.js webpack and socket.io + socket.io-stream. And I am new to this.

I like a lot socket.io-stream because it is easy to use and I all the time get good results. But I do have a bug now.

I have the following code :

import React, { Component} from 'react'
import PropTypes from 'prop-types'
import CustomBannerDropZone from '../../components/tool/BannerDropZone.jsx'

/*
this. socket = io.connect('https://localhost:3000', {
  query: {token: Auth.getToken()}


var socket = io.connect('https://localhost:3000', {
  query: {token: Auth.getToken()}
})
*/
const ss = require('socket.io-stream')

class BannerDropZone extends React.Component {
  constructor (props, context) {
    super(props, context)
    this.maxSize = props.maxSize ? props.maxSize : 2097152
    this.maxWidth = props.maxWidth ? props.maxWidth : 3000
    this.maxHeight = props.maxHeight ? props.maxHeight : 3000
    this.text = props.text ? props.text : `Drop image here. Max ${this.maxSize} and ${this.maxWidth}px by ${this.maxHeight}px`
    this.state = {
      file: null
    }



    this.onDrop = this.onDrop.bind(this)
    this.save = this.save.bind(this)




    ss(this.context.socket).on('file-uploaded', function (data, file) {
      console.log('upload the file')
      this.state.file = file
    })

  }

I have a middleware to check the socket user with JWT, and it is working good for socket.io.
io-auth-check.js

const jwt = require('jsonwebtoken')
const User = require('mongoose').model('User')
const config = require('../../config')

/**
 *  The Auth Checker middleware function.
 */
module.exports = (socket, next) => {
  if (socket.handshake.query && socket.handshake.query.token) {
    jwt.verify(socket.handshake.query.token, config.jwtSecret, (err, decoded) => {
      // the 401 code is for unauthorized status
      if (err)
      {
console.log(err)
        return next(new Error('Authentication error'))
      }

      const userId = decoded.sub

      socket.decoded = decoded
      // check if a user exists
      return User.findById(userId, (userErr, user) => {
        if (userErr || !user) {
          return next(new Error('Authentication error , cannot find the user'))
        }
        console.log(user.familly_name + ' is connected')
        console.log('token thing works fine')
        socket.user = user
        return next()
      })
    })
  }
  next(new Error('Authentication error'))
}

index.js

module.exports = (io) => {

  var mongoose = require('mongoose')
  var ss = require('socket.io-stream')
  var ent = require('ent') // Permet de bloquer les caractères HTML (sécurité équivalente à htmlentities en PHP)
  var Grid = require('gridfs-stream')
  Grid.mongo = mongoose.mongo
  var gfs
  const University = require('mongoose').model('University')

  var ioAuthCheck = require('../middleware/io-auth-check')

  mongoose.connection.once('open', function () {
    gfs = Grid(mongoose.connection.db)
  })

  // Note sure about this

  io.use(ioAuthCheck)
    .on('connection', function (socket) {

The problem is that I got the error specified in the title:

Uncaught Error: Uncaught, unspecified "error" event. (Authentication error)
at Socket.EventEmitter.emit (events.js:62)
at Socket. (index.js:21)
at Socket.Emitter.emit (index.js:133)
at Socket.emit (socket.js:136)
at Socket.onpacket (socket.js:248)
at Manager. (index.js:21)
at Manager.Emitter.emit (index.js:133)
at Manager.ondecoded (manager.js:345)
at Decoder. (index.js:21)
at Decoder.Emitter.emit (index.js:133)
EventEmitter.emit @ events.js:62
(anonymous) @ index.js:21
Emitter.emit @ index.js:133
Socket.emit @ socket.js:136
Socket.onpacket @ socket.js:248
(anonymous) @ index.js:21
Emitter.emit @ index.js:133
Manager.ondecoded @ manager.js:345
(anonymous) @ index.js:21
Emitter.emit @ index.js:133
Decoder.add @ index.js:241
Manager.ondata @ manager.js:335
(anonymous) @ index.js:21
Emitter.emit @ index.js:133
Socket.onPacket @ socket.js:457
(anonymous) @ socket.js:274
Emitter.emit @ index.js:133
Transport.onPacket @ transport.js:145
callback @ polling.js:144
exports.decodePayload @ browser.js:418
Polling.onData @ polling.js:148
(anonymous) @ polling-xhr.js:125
Emitter.emit @ index.js:133
Request.onData @ polling-xhr.js:299
Request.onLoad @ polling-xhr.js:366
xhr.onreadystatechange @ polling-xhr.js:252

If I do disable my middleware, I do not have the error. So it is related to it. I need my middleware . What do you think I could do to resolve this issue ? Is there anything I did wrong ?

Best regards,

Arn

@arn-the-long-beard
Copy link
Author

Hei. Okay it is weird, but to upload a file, I need to push mu button two times, or write into the chat I put on the same page. It looks like I am missing a next() or something . I don't know. I keep trying to figure out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant