Advertisement
Guest User

Untitled

a guest
Sep 17th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. exports.register = async (req, res, next) => {
  2. var profilePicturePath = "";
  3.  
  4. if(!req.files.profilePicture && !req.fields.profilePicture){
  5. req.files.profilePicture = '../../img/icon_default.png';
  6. }else if(!req.files.profilePicture && req.fields.profilePicture){
  7. req.files.profilePicture = '../../img/icon_default.png';
  8. }else{
  9. if(req.files.profilePicture.type != 'application/octet-stream' && req.files.profilePicture.type != 'image/jpeg' &&
  10. req.files.profilePicture.type != 'image/jpg' && req.files.profilePicture.type != 'image/png'){
  11. return next("File Uploaded for Profile is not an image.");
  12. }
  13. }
  14.  
  15. if (req.fields.password !== req.fields.passwordConf) {
  16. var err = new Error('Passwords do not match.');
  17. err.status = 402;
  18. return next(err);
  19. }
  20.  
  21.  
  22. try {
  23.  
  24. if (req.fields.firstname && req.fields.lastname && req.fields.username && req.fields.email
  25. && req.fields.password && req.fields.passwordConf && req.fields.birthDate
  26. && req.fields.gender) //&& req.fields.alias
  27. {
  28.  
  29. let existingUsername = await User.checkUsernameAsync(req.fields.username);
  30.  
  31. if(existingUsername != null){
  32. res.send({ status: 406 });
  33. }
  34.  
  35. let existingEmail = await User.checkEmailAsync(req.fields.email);
  36. if(existingEmail != null){
  37. res.send({ status: 405 });
  38. }
  39.  
  40. let profilePicturePath = await exports.uploaderAsync(req.files.profilePicture);
  41.  
  42. if(!profilePicturePath){
  43. profilePicturePath = '../../img/icon_default.png';
  44. }
  45.  
  46. var userData = new User({
  47. firstname: req.fields.firstname,
  48. surname: req.fields.lastname,
  49. username: req.fields.username,
  50. email: req.fields.email,
  51. password: req.fields.password,
  52. //passwordConf: req.fields.passwordConf,
  53. birthday: req.fields.birthDate,
  54. gender: req.fields.gender,
  55. //aliasImg: aliasPicturePath,
  56. profileImg: profilePicturePath,
  57. });
  58.  
  59. userData.save()
  60. .then(user => {
  61. var token = new Token({ _userId: user._id, token: crypto.randomBytes(16).toString('hex') });
  62. token.save();
  63. return { user: user, token: token };
  64. })
  65.  
  66. .then(savedToken => {
  67.  
  68. var transporter = nodemailer.createTransport({ service: "Gmail",
  69. auth: {
  70. user: credentials.emailAddress,
  71. pass: credentials.password
  72. }
  73. });
  74. var mailOptions = { from: 'no-reply@thinknoel.com', to: savedToken.user.email, subject: 'Account Verification Token', text: 'Hello,\n\n' +
  75. 'Please verify your account by clicking the link: \nhttp:\/\/' + req.headers.host + '\/Register\/confirm?token=' + savedToken.token.token + '.\n' };
  76.  
  77.  
  78. var transRes = transporter.sendMail(mailOptions);
  79. return { transRes: transRes, user: savedToken.user};
  80. })
  81.  
  82. .then(sentMail => {
  83. // console.log(sentMail);
  84. res.send({ status: 200, msg: 'A verification email has been sent to ' + sentMail.user.email + '.'});
  85.  
  86. })
  87.  
  88. .catch(err => {
  89. //console.log(err);
  90. next(err);
  91. });
  92.  
  93.  
  94. }else{
  95. res.send({ status:403, msg: 'All fields required.' });
  96. }
  97.  
  98. } catch(err) {
  99. err => res.status(500).json({ msg: err })
  100. }
  101. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement