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.

160 lines
3.9 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. //PUT - Update a user already exists
  73. exports.updateUser = function(req, res) {
  74. userModel.findById(req.params.id, function(err, user) {
  75. user.username = req.body.username;
  76. user.password = md5(req.body.password);
  77. user.description = req.body.description;
  78. user.avatar = req.body.avatar;
  79. user.mail = req.body.mail;
  80. user.phone = req.body.phone;
  81. user.telegram = req.body.telegram;
  82. user.save(function(err) {
  83. if(err) return res.send(500, err.message);
  84. user.password="";
  85. res.status(200).jsonp(user);
  86. });
  87. });
  88. };
  89. //DELETE - Delete a user with specified ID
  90. exports.deleteUser = function(req, res) {
  91. userModel.findById(req.params.id, function(err, user) {
  92. user.remove(function(err) {
  93. if(err) return res.send(500, err.message);
  94. res.status(200).jsonp(req.params.id);
  95. console.log('DELETE /users/' + req.params.id);
  96. })
  97. });
  98. };
  99. //POST - auth user
  100. exports.login = function(req, res) {
  101. // find the user
  102. userModel.findOne({
  103. username: req.body.username
  104. }, function(err, user) {
  105. if (err) throw err;
  106. if (!user) {
  107. res.json({ success: false, message: 'Authentication failed. User not found.' });
  108. } else if (user) {
  109. req.body.password=md5(req.body.password);
  110. // check if password matches
  111. if (user.password != req.body.password) {
  112. res.json({ success: false, message: 'Authentication failed. Wrong password.' });
  113. } else {
  114. // if user is found and password is right
  115. // create a token
  116. var token = jwt.sign(user, app.get('superSecret'), {
  117. //expiresInMinutes: 1440 // expires in 24 hours
  118. //expiresIn: '60m'
  119. });
  120. console.log(user);
  121. // return the information including token as JSON
  122. res.json({
  123. success: true,
  124. message: 'Enjoy your token!',
  125. token: token,
  126. avatar: user.avatar,
  127. userid: user._id
  128. });
  129. }
  130. }
  131. });
  132. };