tutts / es6-express-mongoose-passport-rest-api

Lightweight boilerplate for Node RESTful API, ES6, Express, Mongoose and Passport 🎁
35 stars 8 forks source link

Application crash if register fails #8

Open oslo10e opened 6 years ago

oslo10e commented 6 years ago

STR: POST: http://localhost:4040/auth/register payload:

{
"email": "a@b.c",
"password": "def"
}

then do the same a second time

Observe log in terminal:

_http_outgoing.js:359
    throw new Error('Can\'t set headers after they are sent.');
    ^

Error: Can't set headers after they are sent.
    at ServerResponse.setHeader (_http_outgoing.js:359:11)
    at ServerResponse.header (/Users/opeaucelle/Sites/test/es6-express-mongoose-passport-rest-api/node_modules/express/lib/response.js:719:10)
    at ServerResponse.send (/Users/opeaucelle/Sites/test/es6-express-mongoose-passport-rest-api/node_modules/express/lib/response.js:164:12)
    at ServerResponse.json (/Users/opeaucelle/Sites/test/es6-express-mongoose-passport-rest-api/node_modules/express/lib/response.js:250:15)
    at /Users/opeaucelle/Sites/test/es6-express-mongoose-passport-rest-api/src/server/controllers/auth.controller.js:31:11
    at /Users/opeaucelle/Sites/test/es6-express-mongoose-passport-rest-api/node_modules/passport/lib/middleware/authenticate.js:236:29
    at /Users/opeaucelle/Sites/test/es6-express-mongoose-passport-rest-api/node_modules/passport/lib/http/request.js:51:48
    at pass (/Users/opeaucelle/Sites/test/es6-express-mongoose-passport-rest-api/node_modules/passport/lib/authenticator.js:287:14)
    at Authenticator.serializeUser (/Users/opeaucelle/Sites/test/es6-express-mongoose-passport-rest-api/node_modules/passport/lib/authenticator.js:289:5)
    at IncomingMessage.req.login.req.logIn (/Users/opeaucelle/Sites/test/es6-express-mongoose-passport-rest-api/node_modules/passport/lib/http/request.js:50:29)
    at Strategy.strategy.success (/Users/opeaucelle/Sites/test/es6-express-mongoose-passport-rest-api/node_modules/passport/lib/middleware/authenticate.js:235:13)
    at verified (/Users/opeaucelle/Sites/test/es6-express-mongoose-passport-rest-api/node_modules/passport-local/lib/strategy.js:83:10)
    at InternalFieldObject.ondone (/Users/opeaucelle/Sites/test/es6-express-mongoose-passport-rest-api/node_modules/passport-local-mongoose/index.js:152:16)
[nodemon] app crashed - waiting for file changes before starting...
oslo10e commented 6 years ago

it could be fixed by adding return in auth.controller.js

    if (err) {
      const error = new APIError('Authentication error', httpStatus.UNAUTHORIZED)
      next(error)
      return
    }
tutts commented 6 years ago

Hi @oslo10e, thanks for reporting! Will take a look in the morning and see whats up, will post PR shortly :)

ssurabhi10 commented 5 years ago

You are sending two response to client. Send only one response to client. If error happens, send error response and if there is success then send success response.