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

calling new RefreshToken from within the model gives problems #125

Closed
gkatsanos opened this issue Dec 16, 2018 · 2 comments
Closed

calling new RefreshToken from within the model gives problems #125

gkatsanos opened this issue Dec 16, 2018 · 2 comments

Comments

@gkatsanos
Copy link
Collaborator

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
Copy link
Collaborator Author

gkatsanos commented Dec 16, 2018

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
Copy link
Owner

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 #334

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

2 participants