Guest User

Untitled

a guest
Dec 3rd, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. const express = require('express')
  2. const bodyParser = require('body-parser')
  3. const mongoose = require('mongoose')
  4. const password = require('password-hash-and-salt')
  5.  
  6. const { Router } = express
  7.  
  8. const app = express()
  9.  
  10. mongoose.connect('mongodb://localhost:27017/hubbers', { useNewUrlParser: true })
  11.  
  12. const User = mongoose.model('User', {
  13. name: String,
  14. email: String,
  15. password: String,
  16. phone: String,
  17. accountType: {
  18. type: String,
  19. enum: ['buyer', 'seller', 'partner'],
  20. default: 'buyer'
  21. },
  22. facebookId: String,
  23. isFacebookAccount: { type: Boolean, default: false }
  24. })
  25.  
  26. app.use(bodyParser.json())
  27.  
  28. const authenticationRouter = Router()
  29.  
  30. const userRouter = Router()
  31.  
  32. authenticationRouter.post('/login', async (req, res) => {
  33. try {
  34. const user = await User.findOne({ email: req.body.email })
  35. if (user) {
  36. password(req.body.password).verifyAgainst(user.password, (err, verified) => {
  37. if (err) throw err
  38. if (verified) {
  39. res.json(user)
  40. } else {
  41. res.status(400)
  42. res.json('Password is invalid')
  43. }
  44. })
  45. } else {
  46. res.status(400)
  47. res.json('Email not found')
  48. }
  49. } catch (err) {
  50. throw err
  51. }
  52. })
  53.  
  54. authenticationRouter.post('/register', async (req, res) => {
  55. try {
  56. const user = await User.findOne({ email: req.body.email })
  57. if (user) {
  58. res.status(400)
  59. res.json('Email already registered')
  60. } else {
  61. password(req.body.password).hash(async (err, hash) => {
  62. if (err) throw err
  63. const newUser = await User.create({
  64. name: req.body.name,
  65. email: req.body.email,
  66. password: hash,
  67. phone: req.body.phone,
  68. type: req.body.accountType
  69. })
  70. res.json(newUser)
  71. })
  72. }
  73. } catch (err) {
  74. throw err
  75. }
  76. })
  77.  
  78. userRouter.put('/password', async (req, res) => {
  79. try {
  80. const user = await User.findById(req.body.id)
  81. if (user) {
  82. password(req.body.password).verifyAgainst(user.password, (err, verified) => {
  83. if (err) throw err
  84. if (verified) {
  85. password(req.body.newPassword).hash((err, hash) => {
  86. if (err) throw err
  87. User.findByIdAndUpdate(req.body.id, {
  88. $set: { password: hash }
  89. }, { new: true }, (err, user) => {
  90. if (err) throw err
  91. res.json('Ok')
  92. })
  93. })
  94. } else {
  95. res.status(400)
  96. res.json('Passwords does not match')
  97. }
  98. })
  99. } else {
  100. res.status(400)
  101. res.json('User ID not found')
  102. }
  103. } catch (err) {
  104. throw err
  105. }
  106. })
  107.  
  108. userRouter.put('/profile', async (req, res) => {
  109. try {
  110. const user = await User.findByIdAndUpdate(req.body.id, {
  111. $set: {
  112. name: req.body.name,
  113. email: req.body.email,
  114. phone: req.body.phone
  115. }
  116. }, { new: true })
  117. res.json(user)
  118. } catch (err) {
  119. throw err
  120. }
  121. })
  122.  
  123. app.use('/authentication', authenticationRouter)
  124. app.use('/user', userRouter)
  125.  
  126. app.listen(process.env.PORT || 3000)
Add Comment
Please, Sign In to add comment