Guest User

Untitled

a guest
Jan 20th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. user.reset_password_token = require('crypto').randomBytes(32).toString('hex');
  2.  
  3. User.findOne({ email: req.body.username }, function(err, user) {
  4.  
  5. crypto.randomBytes(256, function(ex, buf) {
  6. if (ex) throw ex;
  7. user.reset_password_token = buf.toString('hex');
  8. });
  9.  
  10. user.save(); // can I do this here?
  11.  
  12. //will user.reset_password_token be set here??
  13. // Or do I need to put all this code into the randomBytes callback...
  14. //Can I continue programming the .findOne() callback here
  15. // with the expectation that
  16. //user.reset_password_token is set?
  17. //Or am I out of bounds...for the crypto callback to have been called reliably.
  18. });
  19.  
  20. //will user.reset_password_token be set here?
  21.  
  22. User.findOne({ email: req.body.username }, function(err, user) {
  23.  
  24. crypto.randomBytes(256, function(ex, buf) {
  25. if (ex) throw ex;
  26. user.reset_password_token = buf.toString('hex');
  27.  
  28. // only within this scope will user.reset_password_token be
  29. // set as expected
  30. user.save();
  31. });
  32.  
  33. // undefined since this code is called before the crypto call completes
  34. // (at least when you call crypto async like you have)
  35. console.log(user.reset_password_token);
  36. });
Add Comment
Please, Sign In to add comment