You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
5.4 KiB

  1. //File: controllers/userController.js
  2. var mongoose = require('mongoose');
  3. var userModel = mongoose.model('userModel');
  4. var md5 = require('md5');
  5. /* */
  6. var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
  7. var express = require("express");
  8. var app = express();
  9. var config = require('../config'); // get our config file
  10. app.set('superSecret', config.secret); // secret variable
  11. /* */
  12. //GET - Return all Users in the DB
  13. exports.findAllUsers = function(req, res) {
  14. userModel.find(function(err, users) {
  15. if(err) res.send(500, err.message);
  16. //password deletion
  17. for(var i=0; i<users.length; i++)
  18. {
  19. users[i].password="";
  20. console.log(users[i].password);
  21. }
  22. console.log('GET /users');
  23. res.status(200).jsonp(users);
  24. });
  25. };
  26. //GET - Return a User with specified ID
  27. exports.findById = function(req, res) {
  28. userModel.findById(req.params.id, function(err, user) {
  29. if(err) return res.send(500, err.message);
  30. console.log('GET /users/' + req.params.id);
  31. //password deletion
  32. if(user!=null){
  33. user.password="";
  34. }
  35. res.status(200).jsonp(user);
  36. });
  37. };
  38. exports.findUserByUsername = function(req, res) {
  39. userModel.find({
  40. username: req.params.username
  41. }, function(err, user) {
  42. if (err) throw err;
  43. if (!user) {
  44. res.json({ success: false, message: 'no user found' });
  45. } else if (user) {
  46. // return the information including token as JSON
  47. //res.jsonp(user);
  48. user.password="";
  49. console.log(user);
  50. res.status(200).jsonp(user[0]);
  51. }
  52. });
  53. };
  54. //POST - Insert a new User in the DB
  55. exports.addUser = function(req, res) {
  56. console.log('POST new user, name: ' + req.body.username);
  57. //console.log(req.body);
  58. var user = new userModel({
  59. username: req.body.username,
  60. password: md5(req.body.password),
  61. description: req.body.description,
  62. avatar: req.body.avatar,
  63. mail: req.body.mail,
  64. phone: req.body.phone,
  65. telegram: req.body.telegram
  66. });
  67. user.save(function(err, user) {
  68. if(err) return res.send(500, err.message);
  69. res.status(200).jsonp(user);
  70. });
  71. };
  72. /* fav */
  73. exports.addFav = function(req, res) {
  74. userModel.findById(req.params.userId, function(err, user){
  75. // first search if user have already said like
  76. var favRepeated=false;
  77. for(var i=0; i<user.favs.length; i++)
  78. {
  79. if(user.favs[i].username==req.body.username)
  80. {
  81. favRepeated=true;
  82. }
  83. }
  84. console.log("favRepeated: " + favRepeated);
  85. if(favRepeated==false)
  86. {
  87. var fav = {
  88. userId: req.body.userId,
  89. username: req.body.username,
  90. avatar: req.body.avatar
  91. };
  92. user.favs.push(fav);
  93. user.save(function(err, user) {
  94. if(err) return res.send(500, err.message);
  95. //res.status(200).jsonp(travel);
  96. userModel.find(function(err, users) {
  97. if(err) res.send(500, err.message);
  98. res.status(200).jsonp(users);
  99. });
  100. });
  101. }else{
  102. userModel.find(function(err, users) {
  103. if(err) res.send(500, err.message);
  104. res.status(200).jsonp(users);
  105. });
  106. }
  107. });
  108. };
  109. exports.doUnfav = function(req, res) {
  110. userModel.findById(req.params.userId, function(err, user){
  111. for(var i=0; i<user.favs.length; i++)
  112. {
  113. if(user.favs[i].username==req.body.username)
  114. {
  115. user.favs.splice(i, 1);
  116. }
  117. }
  118. user.save(function(err, travel) {
  119. if(err) return res.send(500, err.message);
  120. //res.status(200).jsonp(travel);
  121. userModel.find(function(err, users) {
  122. if(err) res.send(500, err.message);
  123. res.status(200).jsonp(users);
  124. });
  125. });
  126. });
  127. };
  128. //PUT - Update a user already exists
  129. exports.updateUser = function(req, res) {
  130. userModel.findById(req.params.id, function(err, user) {
  131. user.username = req.body.username;
  132. user.password = md5(req.body.password);
  133. user.description = req.body.description;
  134. user.avatar = req.body.avatar;
  135. user.mail = req.body.mail;
  136. user.phone = req.body.phone;
  137. user.telegram = req.body.telegram;
  138. user.save(function(err) {
  139. if(err) return res.send(500, err.message);
  140. user.password="";
  141. res.status(200).jsonp(user);
  142. });
  143. });
  144. };
  145. //DELETE - Delete a user with specified ID
  146. exports.deleteUser = function(req, res) {
  147. userModel.findById(req.params.id, function(err, user) {
  148. user.remove(function(err) {
  149. if(err) return res.send(500, err.message);
  150. res.status(200).jsonp(req.params.id);
  151. console.log('DELETE /users/' + req.params.id);
  152. })
  153. });
  154. };
  155. //POST - auth user
  156. exports.login = function(req, res) {
  157. // find the user
  158. userModel.findOne({
  159. username: req.body.username
  160. }, function(err, user) {
  161. if (err) throw err;
  162. if (!user) {
  163. res.json({ success: false, message: 'Authentication failed. User not found.' });
  164. } else if (user) {
  165. req.body.password=md5(req.body.password);
  166. // check if password matches
  167. if (user.password != req.body.password) {
  168. res.json({ success: false, message: 'Authentication failed. Wrong password.' });
  169. } else {
  170. // if user is found and password is right
  171. // create a token
  172. var token = jwt.sign(user, app.get('superSecret'), {
  173. //expiresInMinutes: 1440 // expires in 24 hours
  174. //expiresIn: '60m'
  175. });
  176. console.log(user);
  177. // return the information including token as JSON
  178. user.password="";
  179. res.json({
  180. success: true,
  181. message: 'Enjoy your token!',
  182. token: token,
  183. avatar: user.avatar,
  184. userid: user._id,
  185. userdata: user
  186. });
  187. }
  188. }
  189. });
  190. };