Guest User

Untitled

a guest
Dec 28th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. methods.signup = async (req,res,next) => {
  2. const errors = validationResult(req);
  3. if (!errors.isEmpty()) {
  4. return res.status(config.errorCodes.validation).json({errors:errors.array()});
  5. }
  6. let payload = req.body;
  7. var newUser = new User({
  8. username:payload.username,
  9. fullname:payload.fullname,
  10. email: payload.email,
  11. password: payload.password,
  12. urltoken:{
  13. token:null
  14. },
  15. actStatus: 0,
  16. provider: 'email',
  17. role:1
  18. });
  19. let hash = commonMethods.generatePasswordHash(payload.password);
  20. let token = commonMethods.createRandomString();
  21.  
  22. let [a,b] = await Promise.all([hash,token]).catch(err=>{
  23. res.status(400).json({status:false,msg:"error in promise"})
  24. });
  25.  
  26. newUser.password = a;
  27. newUser.urltoken.token = b;
  28. newUser.save(function(err){
  29. if(err) {
  30. return res.status(config.errorCodes.success).json({ status:false
  31. ,msg:"Error in saving userdata. Please try again",err});
  32. }
  33. else {
  34. commonMethods.sendMail(newUser);
  35. return res.status(config.errorCodes.success).json({"status":true,
  36. "msg":"Registered succesfully. Please Check your Email to verify
  37. your account.",newUser})
  38. }
  39. });
  40. }
  41.  
  42. commonMethods.createRandomString = function(password){
  43. return new Promise(function(resolve, reject) {
  44. var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
  45. var string_length = 25;
  46. var randomstring = '';
  47. for (var i=0; i<string_length; i++) {
  48. var rnum = Math.floor(Math.random() * chars.length);
  49. randomstring += chars.substring(rnum,rnum+1);
  50. }
  51. reject(randomstring);
  52. // resolve(randomstring);
  53.  
  54. })
  55. }
  56.  
  57. UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
  58. (node:15172) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Add Comment
Please, Sign In to add comment