Guest User

Untitled

a guest
Feb 13th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. router.post('/user/signup',
  2. AuthenticationController.signupWithCredentials)
  3.  
  4. 'use strict'
  5.  
  6. const { Router } = require('express')
  7. const { TestController } = require('./../controllers/TestController')
  8. const { AuthenticationController } = require('./../controllers/AuthenticationController')
  9. const { AuthMiddleware } = require('./../middleware/AuthMiddleware')
  10.  
  11. const router = new Router()
  12.  
  13. router.get('/test', TestController.test)
  14.  
  15. router.post('/user/signup', AuthenticationController.signupWithCredentials)
  16.  
  17. module.exports = router
  18.  
  19. 'use strict'
  20. const uuid = require('uuid/v4')
  21. const bcrypt = require('bcrypt')
  22. const config = require('./../config')
  23.  
  24. const { promiseEjs } = require('./../utils/promiseEjs')
  25. const { mailSender } = require('./../utils/mailSender')
  26. const { errorHandler } = require('./../utils/errorHandler')
  27. const { User } = require('./../schema/user')
  28. const { authMiddleWare } = require('./../middleware/AuthMiddleware')
  29.  
  30. class AuthenticationController {
  31. /**
  32. * API 1.0 | POST
  33. * @example {
  34. * firstName: String,
  35. * lastName: String,
  36. * email: String,
  37. * password: String
  38. * }
  39. * @param {*} req
  40. * @param {*} res
  41. */
  42. static async signupWithCredentials (req, res) {
  43. try {
  44. let email = req.body.email
  45. let user = await User.findOne({ email: email })
  46. if (user) {
  47. throw {
  48. code: 400, message: 'User already exist'
  49. }
  50. }
  51. else {
  52. let emailVerificationToken = uuid()
  53. user = new User({
  54. firstName: req.body.firstName,
  55. lastName: req.body.lastName,
  56. email: req.body.email,
  57. password: bcrypt.hashSync(req.body.password, 8),
  58. emailVerificationToken: emailVerificationToken
  59. })
  60. await user.save()
  61. try {
  62. let emailVerificationLink = `${config.SERVER_DEV_URL}/authentication/verify?token=${emailVerificationToken}&user=${user._id}`
  63. if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'staging') { emailVerificationLink = `${config.SERVER_PRODUCTION_URL}/authentication/verify?token=${emailVerificationToken}&user=${user._id}` }
  64. let html = await promiseEjs.renderFile('./emails/signup.ejs', { user: user, emailVerificationLink: emailVerificationLink })
  65. await mailSender.sendMail(user.email, 'Welcome to HandyGems', html)
  66. }
  67. catch (error) {
  68. console.log(error)
  69. }
  70. let authToken = authMiddleWare.createJWT(user)
  71. await User.findOneAndUpdate({ _id: user._id }, { $set: { authToken: authToken } })
  72. res.send({ authToken: authToken })
  73. }
  74. }
  75. catch (error) {
  76. errorHandler.sendError(res, error)
  77. }
  78. }
  79. }
  80.  
  81. const authenticationController = new AuthenticationController()
  82. module.exports = { authenticationController }
  83.  
  84. class AuthenticationController {
  85. static async signupWithCredentials (req, res) {
  86. console.log('you are just calling static method')
  87. }
  88. withoutStaticMethod(req, res) {
  89. console.log('you are just calling non static method using object')
  90. }
  91. }
  92. module.exports = AuthenticationController
  93.  
  94. const AuthenticationController = require('./AuthenticationController')
  95. router.get('/signup', AuthenticationController.signupWithCredentials)
  96.  
  97. const obj = new AuthenticationController()
  98. router.get('/testing2', obj.withoutStaticMethod)
Add Comment
Please, Sign In to add comment