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.

204 lines
5.0 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. var fav = {
  76. userId: req.body.userId,
  77. username: req.body.username,
  78. avatar: req.body.avatar
  79. };
  80. user.favs.push(fav);
  81. user.save(function(err, user) {
  82. if(err) return res.send(500, err.message);
  83. //res.status(200).jsonp(travel);
  84. userModel.find(function(err, users) {
  85. if(err) res.send(500, err.message);
  86. res.status(200).jsonp(users);
  87. });
  88. });
  89. });
  90. };
  91. exports.doUnfav = function(req, res) {
  92. userModel.findById(req.params.userId, function(err, user){
  93. for(var i=0; i<user.favs.length; i++)
  94. {
  95. if(user.favs[i].username==req.body.username)
  96. {
  97. user.favs.splice(i, 1);
  98. }
  99. }
  100. user.save(function(err, travel) {
  101. if(err) return res.send(500, err.message);
  102. //res.status(200).jsonp(travel);
  103. userModel.find(function(err, users) {
  104. if(err) res.send(500, err.message);
  105. res.status(200).jsonp(users);
  106. });
  107. });
  108. });
  109. };
  110. //PUT - Update a user already exists
  111. exports.updateUser = function(req, res) {
  112. userModel.findById(req.params.id, function(err, user) {
  113. user.username = req.body.username;
  114. user.password = md5(req.body.password);
  115. user.description = req.body.description;
  116. user.avatar = req.body.avatar;
  117. user.mail = req.body.mail;
  118. user.phone = req.body.phone;
  119. user.telegram = req.body.telegram;
  120. user.save(function(err) {
  121. if(err) return res.send(500, err.message);
  122. user.password="";
  123. res.status(200).jsonp(user);
  124. });
  125. });
  126. };
  127. //DELETE - Delete a user with specified ID
  128. exports.deleteUser = function(req, res) {
  129. userModel.findById(req.params.id, function(err, user) {
  130. user.remove(function(err) {
  131. if(err) return res.send(500, err.message);
  132. res.status(200).jsonp(req.params.id);
  133. console.log('DELETE /users/' + req.params.id);
  134. })
  135. });
  136. };
  137. //POST - auth user
  138. exports.login = function(req, res) {
  139. // find the user
  140. userModel.findOne({
  141. username: req.body.username
  142. }, function(err, user) {
  143. if (err) throw err;
  144. if (!user) {
  145. res.json({ success: false, message: 'Authentication failed. User not found.' });
  146. } else if (user) {
  147. req.body.password=md5(req.body.password);
  148. // check if password matches
  149. if (user.password != req.body.password) {
  150. res.json({ success: false, message: 'Authentication failed. Wrong password.' });
  151. } else {
  152. // if user is found and password is right
  153. // create a token
  154. var token = jwt.sign(user, app.get('superSecret'), {
  155. //expiresInMinutes: 1440 // expires in 24 hours
  156. //expiresIn: '60m'
  157. });
  158. console.log(user);
  159. // return the information including token as JSON
  160. user.password="";
  161. res.json({
  162. success: true,
  163. message: 'Enjoy your token!',
  164. token: token,
  165. avatar: user.avatar,
  166. userid: user._id,
  167. userdata: user
  168. });
  169. }
  170. }
  171. });
  172. };