Simplecode Community Forums

Create a REST API [Part 7]: Forgot / Reset Password Routes

Security Reminder: Although we do our best to ensure our tutorials follow security best practices. It is up to you to do your research and make sure the code you implement in your application is secure.


This is a companion discussion topic for the original entry at https://www.simplecode.io/blog/create-a-rest-api-part-7-forgot-reset-password-routes

Hi @Owen,

Looks like running the reset_password route crashes the app with this error:

Unhandled rejection Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:467:11)
    at ServerResponse.header (/Users/prisc_000/Downloads/simple-api-master/node_modules/express/lib/response.js:767:10)
    at ServerResponse.send (/Users/prisc_000/Downloads/simple-api-master/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/Users/prisc_000/Downloads/simple-api-master/node_modules/express/lib/response.js:267:15)
    at /Users/prisc_000/Downloads/simple-api-master/api/routes/users.js:324:33
    at tryCatcher (/Users/prisc_000/Downloads/simple-api-master/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/prisc_000/Downloads/simple-api-master/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/Users/prisc_000/Downloads/simple-api-master/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/Users/prisc_000/Downloads/simple-api-master/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/Users/prisc_000/Downloads/simple-api-master/node_modules/bluebird/js/release/promise.js:690:18)
    at _drainQueueStep (/Users/prisc_000/Downloads/simple-api-master/node_modules/bluebird/js/release/async.js:138:12)
    at _drainQueue (/Users/prisc_000/Downloads/simple-api-master/node_modules/bluebird/js/release/async.js:131:9)
    at Async._drainQueues (/Users/prisc_000/Downloads/simple-api-master/node_modules/bluebird/js/release/async.js:147:5)
    at Immediate.Async.drainQueues [as _onImmediate] (/Users/prisc_000/Downloads/simple-api-master/node_modules/bluebird/js/release/async.js:17:14)
    at processImmediate (internal/timers.js:439:21)
    at process.topLevelDomainCallback (domain.js:126:23)

This is happening on the master repo after running npm install add configuring postgres and ses. I can’t get my head around what is happening though I reread this stackovflow post a few times. Is this expected?

Thank you.

Hey @idkjs, this shouldn’t happen. Give me a minute to look into this and I’ll post back here soon.:+1:

:woozy_face: The error was in our code at this line of the reset_password route:

.then(user => {
            const subject = "Password change for your account.";
            const content = `The password for your account registered under ${
              user[0]
            } has been successfully changed.`;
            res.json("Password successfully changed for " + user[0] + "!");

            sendEmail.Email(to, subject, content); // <------ This is the problem
          })

after res.json, you shouldn’t run any more code or you get that error. If you move

sendEmail.Email(to, subject, content);

above res.json like so you won’t get that error:

.then(user => {
            const subject = "Password change for your account.";
            const content = `The password for your account registered under ${
              user[0]
            } has been successfully changed.`;

            sendEmail.Email(to, subject, content);
            res.json("Password successfully changed for " + user[0] + "!");
          })

Hope this helps! I’ll fix it in the tutorial and the github repo too :wink: Thanks for pointing it out!

1 Like