Guest User

Untitled

a guest
Dec 13th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. var smtpTransport = nodemailer.createTransport({
  2. service: "Gmail",
  3. auth: {
  4. user: "gmail",
  5. pass: "password"
  6. }
  7. });
  8. var rand,mailOptions,host,link;
  9.  
  10. router.post('/register', AuthCtrl.CreateUser);
  11. router.post('/login', AuthCtrl.LoginUser);
  12. router.get('/send',function(req,res){
  13. rand=Math.floor((Math.random() * 100) + 54);
  14. host=req.get('host');
  15. link="http://"+req.get('host')+"/verify?id="+rand;
  16. mailOptions={
  17. to : req.query.to,
  18. subject : "Please confirm your Email account",
  19. html : "Hello,<br> Please Click on the link to verify your email.<br><a href="+link+">Click here to verify</a>"
  20. }
  21. console.log(mailOptions);
  22. smtpTransport.sendMail(mailOptions, function(error, response){
  23. if(error){
  24. console.log(error);
  25. res.end("error");
  26. }else{
  27. console.log("Message sent: " + response.message);
  28. res.end("sent");
  29. }
  30. });
  31. });
  32.  
  33. router.get('/verify',function(req,res){
  34. console.log(req.protocol+":/"+req.get('host'));
  35. if((req.protocol+"://"+req.get('host'))==("http://"+host))
  36. {
  37. console.log("Domain is matched. Information is from Authentic email");
  38. if(req.query.id==rand)
  39. {
  40. console.log("email is verified");
  41. res.end("<h1>Email "+mailOptions.to+" is been Successfully verified");
  42. }
  43. else
  44. {
  45. console.log("email is not verified");
  46. res.end("<h1>Bad Request</h1>");
  47. }
  48. }
  49. else
  50. {
  51. res.end("<h1>Request is from unknown source");
  52. }
  53. });
  54.  
  55. module.exports = router;
  56.  
  57. module.exports = {
  58. async CreateUser(req, res) {
  59. const schema = Joi.object().keys({
  60. username: Joi.string()
  61. .required(),
  62. email: Joi.string()
  63. .email()
  64. .required(),
  65. password: Joi.string()
  66. .required(),
  67. birthday: Joi.number().integer()
  68. .required().min(2).max(2),
  69. birthmonth: Joi.number().integer()
  70. .required().min(2).max(2),
  71. birthyear: Joi.number().integer()
  72. .required(),
  73. age:age
  74. });
  75.  
  76. const { error, value } = Joi.validate(req.body, schema);
  77. if (error && error.details) {
  78. return res.status(HttpStatus.BAD_REQUEST).json({ msg: error.details })
  79. }
  80.  
  81. const userEmail = await User.findOne({
  82. email: Helpers.lowerCase(req.body.email)
  83. });
  84. if (userEmail) {
  85. return res
  86. .status(HttpStatus.CONFLICT)
  87. .json({ message: 'Email already exist' });
  88. }
  89.  
  90. const userName = await User.findOne({
  91. username: Helpers.firstUpper(req.body.username)
  92. });
  93. if (userName) {
  94. return res
  95. .status(HttpStatus.CONFLICT)
  96. .json({ message: 'Username already exist' });
  97. }
  98.  
  99. return bcrypt.hash(value.password, 10, (err, hash) => {
  100. if (err) {
  101. return res
  102. .status(HttpStatus.BAD_REQUEST)
  103. .json({ message: 'Error hashing password' });
  104. }
  105. const age = moment().diff(moment([birthyear, birthmonth - 1, birthday]), 'years');
  106.  
  107. const body = {
  108. username: Helpers.firstUpper(value.username),
  109. email: Helpers.lowerCase(value.email),
  110. birthday: (value.bday),
  111. birthmonth: (value.month),
  112. birthyear: (value.month),
  113. password: hash,
  114. age:age
  115. };
  116. User.create(body)
  117. .then(user => {
  118. const token = jwt.sign({ data: user }, dbConfig.secret, {
  119. expiresIn: '5h'
  120. });
  121. res.cookie('auth', token);
  122. res
  123. .status(HttpStatus.CREATED)
  124. .json({ message: 'User created successfully', user, token });
  125. })
  126. .catch(err => {
  127. res
  128. .status(HttpStatus.INTERNAL_SERVER_ERROR)
  129. .json({ message: 'Error occured' });
  130. });
  131. });
  132. },
Add Comment
Please, Sign In to add comment