danielfsousa / express-rest-boilerplate

⌛️ Express starter for building RESTful APIs
MIT License
2.36k stars 624 forks source link

calling new RefreshToken from within the model gives problems #125

Closed gkatsanos closed 4 years ago

gkatsanos commented 5 years ago

https://github.com/danielfsousa/express-rest-es2017-boilerplate/blob/032a005deaa68a06f476432f4d602917609a251a/src/api/models/refreshToken.model.js#L41

Hey Daniel, how's it going? I was cleaning up my API based on your boilerplate, and after changing the export to be module.exports = mongoose.model('RefreshToken', refreshTokenSchema); instead of

const RefreshToken = mongoose.model('RefreshToken', refreshTokenSchema);
module.exports = RefreshToken;

it broke (naturally) - I was thinking of we could improve this a bit by moving the new declaration in the controller?

gkatsanos commented 5 years ago

PS: I was actually debugging a bug I had - when I retry a refreshToken, I get an error (expected I guess) but the error isn't handled properly..

exports.refresh = async (req, res, next) => {
  try {
    const { email, refreshToken } = req.body;
    const refreshObject = await RefreshToken.findOneAndRemove({
      userEmail: email,
      token: refreshToken,
    });
    const { user, accessToken } = await User.findAndGenerateToken({ email, refreshObject });
    const response = generateTokenResponse(user, accessToken);
    return res.json(response);
  } catch (error) {
    return next(error);
  }
};
"Error: Illegal arguments: undefined, string\n    at _async (/app/node_modules/bcryptjs/dist/bcrypt.js:286:46)\n    at /app/node_modules/bcryptjs/dist/bcrypt.js:307:17\n    at Promise._execute (/app/node_modules/bluebird/js/release/debuggability.js:313:9)\n    at Promise._resolveFromExecutor (/app/node_modules/bluebird/js/release/promise.js:483:18)\n    at new Promise (/app/node_modules/bluebird/js/release/promise.js:79:10)\n    at Object.bcrypt.compare (/app/node_modules/bcryptjs/dist/bcrypt.js:306:20)\n    at model.passwordMatches (/app/src/models/user.model.js:98:19)\n    at Function.findAndGenerateToken (/app/src/models/user.model.js:126:28)"

thrown by User.findAndGenerateToken({ email, refreshObject }); (refreshObject is null)

danielfsousa commented 4 years ago

Hey George, I will close this old issue because we will probably switch to Postgres on the next version of the boilerplate.

We can talk about the current problems of the boilerplate and check the progress on https://github.com/danielfsousa/express-rest-boilerplate/issues/334