Advertisement
Guest User

Untitled

a guest
Apr 8th, 2017
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. module.exports = function (app, models, password, fs, emailServer) {
  2. app.post('/users', function (req, res) {
  3. models.User.findOne({$or: [{
  4. email: req.body.email
  5. }, {
  6. phone: req.body.phone
  7. }]}, function (err, user) {
  8. if (err) throw err;
  9. if (user) {
  10. res.status(400);
  11. user.email === req.body.email ? res.send('Email already taken.') : res.send('Phone already taken.');
  12. } else {
  13. password(req.body.password).hash(function (err, password) {
  14. if (err) throw err;
  15. function capitalizeName (name) {
  16. return name
  17. .toLowerCase()
  18. .split(' ')
  19. .map(function (word) {
  20. return word[0].toUpperCase() + word.substr(1);
  21. })
  22. .join(' ')
  23. };
  24. models.User.create({
  25. name: capitalizeName(req.body.name),
  26. email: req.body.email,
  27. phone: req.body.phone,
  28. password: password,
  29. photo: 'images/user-default.png'
  30. }, function (err, user) {
  31. if (err) throw err;
  32. res.json(user);
  33. });
  34. });
  35. };
  36. });
  37. });
  38.  
  39. app.post('/users/login', function (req, res) {
  40. models.User.findOne({
  41. email: req.body.email
  42. }, function (err, user) {
  43. if (err) throw err;
  44. if (user) {
  45. password(req.body.password).verifyAgainst(user.password, function (err, verified) {
  46. if (err) throw err;
  47. if (verified) {
  48. res.json(user);
  49. } else {
  50. res.status(400);
  51. res.send('Invalid login.');
  52. };
  53. });
  54. } else {
  55. res.status(400);
  56. res.send('Invalid login.');
  57. };
  58. });
  59. });
  60.  
  61. app.put('/users/photo', function (req, res) {
  62. var fileName = req.body.id + new Date().getTime() + '.png';
  63. fs.writeFile('public/photos/' + fileName, req.body.photo, 'base64', function (err) {
  64. if (err) throw err;
  65. models.User.findOneAndUpdate({
  66. _id: req.body.id
  67. }, {$set: {
  68. photo: 'https://api.liffy.com.br/photos/' + fileName
  69. }}, {new: true}, function (err, user) {
  70. if (err) throw err;
  71. res.json(user);
  72. });
  73. });
  74. });
  75.  
  76. app.put('/users/remove-photo', function (req, res) {
  77. models.User.findOneAndUpdate({
  78. _id: req.body.id
  79. }, {$set: {
  80. photo: 'images/user-default.png'
  81. }}, {new: true}, function (err, user) {
  82. if (err) throw err;
  83. res.json(user);
  84. });
  85. });
  86.  
  87. app.put('/users/password', function (req, res) {
  88. models.User.findOne({
  89. _id: req.body.id
  90. }, function (err, user) {
  91. if (err) throw err;
  92. password(req.body.password).verifyAgainst(user.password, function (err, verified) {
  93. if (err) throw err;
  94. if (verified) {
  95. password(req.body.newPassword).hash(function (err, password) {
  96. if (err) throw err;
  97. models.User.findOneAndUpdate({
  98. _id: req.body.id
  99. }, {$set: {
  100. password: password,
  101. }}, {new: true}, function (err, user) {
  102. if (err) throw err;
  103. res.json(user);
  104. });
  105. });
  106. } else {
  107. res.status(400);
  108. res.send('Invalid password.');
  109. };
  110. });
  111. });
  112. });
  113.  
  114. app.put('/users/email', function (req, res) {
  115. models.User.findOne({
  116. email: req.body.email
  117. }, function (err, user) {
  118. if (err) throw err;
  119. if (user) {
  120. res.status(400);
  121. res.send('Email already taken.');
  122. } else {
  123. models.User.findOne({
  124. _id: req.body.id
  125. }, function (err, user) {
  126. if (err) throw err;
  127. password(req.body.password).verifyAgainst(user.password, function (err, verified) {
  128. if (err) throw err;
  129. if (verified) {
  130. models.User.findOneAndUpdate({
  131. _id: req.body.id
  132. }, {$set: {
  133. email: req.body.email,
  134. }}, {new: true}, function (err, user) {
  135. if (err) throw err;
  136. res.json(user);
  137. });
  138. } else {
  139. res.status(400);
  140. res.send('Invalid password.');
  141. };
  142. });
  143. });
  144. };
  145. });
  146. });
  147.  
  148. app.get('/users/:id/travels', function (req, res) {
  149. models.Travel.find({
  150. user: req.params.id,
  151. showToUser: true,
  152. done: true
  153. })
  154. .populate('driver')
  155. .exec(function (err, travels) {
  156. if (err) throw err;
  157. res.json(travels);
  158. });
  159. });
  160.  
  161. app.delete('/users/travels/:id', function (req, res) {
  162. models.Travel.findOneAndUpdate({
  163. _id: req.params.id
  164. }, {$set: {
  165. showToUser: false
  166. }}, function (err, travel) {
  167. if (err) throw err;
  168. models.Travel.find({
  169. user: travel.user,
  170. showToUser: true,
  171. done: true
  172. })
  173. .populate('driver')
  174. .exec(function (err, travels) {
  175. if (err) throw err;
  176. res.json(travels);
  177. });
  178. });
  179. });
  180.  
  181. app.post('/users/recover-password', function (req, res) {
  182. models.User.findOne({
  183. email: req.body.email
  184. }, function (err, user) {
  185. if (err) throw err;
  186. var _code = (Math.floor(Math.random() * (9 - 1)) + 1).toString();
  187. if (user) {
  188. for (i = 0; i < 3; i++) {
  189. var _randomNumber = Math.floor(Math.random() * (9 - 1)) + 1;
  190. var _code = _code + _randomNumber.toString();
  191. };
  192. emailServer.send({
  193. text: 'Utilize ' + _code + ' para recuperar a sua senha no Liffy.',
  194. from: 'Liffy <liffyapp@gmail.com>',
  195. to: user.email + '<' + user.email + '>',
  196. subject: 'Seu código Liffy'
  197. }, function (err, details) {
  198. if (err) throw err;
  199. res.json({
  200. id: user._id,
  201. code: _code
  202. });
  203. });
  204. } else {
  205. res.status(400);
  206. res.send('Invalid email.');
  207. };
  208. });
  209. });
  210.  
  211. app.put('/users/set-new-password', function (req, res) {
  212. password(req.body.password).hash(function (err, password) {
  213. if (err) throw err;
  214. models.User.findOneAndUpdate({
  215. _id: req.body.id
  216. }, {$set: {
  217. password: password
  218. }}, {new: true}, function (err, user) {
  219. if (err) throw err;
  220. res.json(user);
  221. });
  222. });
  223. });
  224.  
  225. app.get('/users/:id', function (req, res) {
  226. models.User.findOne({
  227. _id: req.params.id
  228. }, function (err, user) {
  229. if (err) throw err;
  230. res.json(user);
  231. });
  232. });
  233.  
  234. app.post('/users/fb-login', function (req, res) {
  235. models.User.findOne({
  236. email: req.body.email
  237. }, function (err, user) {
  238. if (err) throw err;
  239. if (user) {
  240. res.json(user);
  241. } else {
  242. res.end();
  243. };
  244. });
  245. });
  246.  
  247. app.post('/users/fb-sign', function (req, res) {
  248. models.User.findOne({
  249. phone: req.body.phone
  250. }, function (err, user) {
  251. if (err) throw err;
  252. if (user) {
  253. res.status(400);
  254. res.send('Phone already taken');
  255. } else {
  256. req.body.photo.silhouette ? _photo = 'images/user-default.png' : _photo = req.body.photo.url;
  257. models.User.create({
  258. name: req.body.name,
  259. email: req.body.email,
  260. phone: req.body.phone,
  261. photo: _photo
  262. }, function (err, user) {
  263. if (err) throw err;
  264. res.json(user);
  265. });
  266. };
  267. });
  268. });
  269. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement