0
function isAuthenticated() {
    return compose()
        .use(function(req, res, next) {
            if (!req.headers.authorization) {
                return res.status(200).send({ status: 'failure', data: [], message: 'Please login to perform this action' });
            }
            try {
                const token = req.headers.authorization;
                const decoded = jwt.verify(token, process.env.SECRET_KEY);

                User.findOne({ where: { id: decoded.id } }).then(userFound => {
                    if (userFound) {
                        req.user = userFound;
                        next();
                    } else {
                        return res.json({ status: 'failure', data: [], message: 'Unauthorised.' });
                    }
                }).catch(e => {
                    return res.json({ status: 'failure', data: [], message: 'Unauthorised' });
                });
            } catch (error) {
                return res.json({ status: 'failure', data: [], message: 'Authentication failed' + error.message });
            }
        });
};

It shows warning like:

(node:9900) Warning: a promise was created in a handler at /home/nodejs/server/auth/auth.service.js:23:25 but was not returned from it, see

How can I handle these warnings, I tried with return statement at different places but it has not resolved.

norbitrial
  • 14,716
  • 7
  • 32
  • 59

1 Answers1

0

You missed a return statement from the following part:

User.findOne({ where: { id: decoded.id } }).then(userFound => {
   if (userFound) {
      req.user = userFound;
      next();
      // I guess here the code should return as well
   } else {
      return res.json({ status: 'failure', data: [], message: 'Unauthorised.' });
   }
}

From the docs:

This usually means that you simply forgot a return statement somewhere, which will cause a runaway promise that is not connected to any promise chain.

Please find the source here: Warning: a promise was created in a handler but was not returned from it

norbitrial
  • 14,716
  • 7
  • 32
  • 59