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.

154 lines
3.8 KiB

8 years ago
  1. //File: controllers/userController.js
  2. var mongoose = require('mongoose');
  3. var userModel = mongoose.model('userModel');
  4. /* */
  5. var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
  6. var express = require("express");
  7. var app = express();
  8. var config = require('../config'); // get our config file
  9. app.set('superSecret', config.secret); // secret variable
  10. /* */
  11. //GET - Return all Users in the DB
  12. exports.findAllUsers = function(req, res) {
  13. userModel.find(function(err, users) {
  14. if(err) res.send(500, err.message);
  15. //password deletion
  16. for(var i=0; i<users.length; i++)
  17. {
  18. users[i].password="";
  19. console.log(users[i].password);
  20. }
  21. console.log('GET /users');
  22. res.status(200).jsonp(users);
  23. });
  24. };
  25. //GET - Return a User with specified ID
  26. exports.findById = function(req, res) {
  27. userModel.findById(req.params.id, function(err, user) {
  28. if(err) return res.send(500, err.message);
  29. console.log('GET /users/' + req.params.id);
  30. //password deletion
  31. user.password="";
  32. res.status(200).jsonp(user);
  33. });
  34. };
  35. exports.findUserByUsername = function(req, res) {
  36. userModel.find({
  37. username: req.params.username
  38. }, function(err, user) {
  39. if (err) throw err;
  40. if (!user) {
  41. res.json({ success: false, message: 'no user found' });
  42. } else if (user) {
  43. // return the information including token as JSON
  44. //res.jsonp(user);
  45. user.password="";
  46. console.log(user);
  47. res.status(200).jsonp(user[0]);
  48. }
  49. });
  50. };
  51. //POST - Insert a new User in the DB
  52. exports.addUser = function(req, res) {
  53. console.log('POST new user, name: ' + req.body.username);
  54. //console.log(req.body);
  55. var user = new userModel({
  56. username: req.body.username,
  57. password: req.body.password,
  58. description: req.body.description,
  59. avatar: req.body.avatar,
  60. mail: req.body.mail,
  61. phone: req.body.phone,
  62. telegram: req.body.telegram
  63. });
  64. user.save(function(err, user) {
  65. if(err) return res.send(500, err.message);
  66. res.status(200).jsonp(user);
  67. });
  68. };
  69. //PUT - Update a user already exists
  70. exports.updateUser = function(req, res) {
  71. userModel.findById(req.params.id, function(err, user) {
  72. user.username = req.body.username;
  73. user.password = req.body.password;
  74. user.description = req.body.description;
  75. user.avatar = req.body.avatar;
  76. user.mail = req.body.mail;
  77. user.phone = req.body.phone;
  78. user.telegram = req.body.telegram;
  79. user.save(function(err) {
  80. if(err) return res.send(500, err.message);
  81. user.password="";
  82. res.status(200).jsonp(user);
  83. });
  84. });
  85. };
  86. //DELETE - Delete a user with specified ID
  87. exports.deleteUser = function(req, res) {
  88. userModel.findById(req.params.id, function(err, user) {
  89. user.remove(function(err) {
  90. if(err) return res.send(500, err.message);
  91. res.status(200).jsonp(req.params.id);
  92. console.log('DELETE /users/' + req.params.id);
  93. })
  94. });
  95. };
  96. //POST - auth user
  97. exports.login = function(req, res) {
  98. // find the user
  99. userModel.findOne({
  100. username: req.body.username
  101. }, function(err, user) {
  102. if (err) throw err;
  103. if (!user) {
  104. res.json({ success: false, message: 'Authentication failed. User not found.' });
  105. } else if (user) {
  106. // check if password matches
  107. if (user.password != req.body.password) {
  108. res.json({ success: false, message: 'Authentication failed. Wrong password.' });
  109. } else {
  110. // if user is found and password is right
  111. // create a token
  112. var token = jwt.sign(user, app.get('superSecret'), {
  113. //expiresInMinutes: 1440 // expires in 24 hours
  114. expiresIn: '60m'
  115. });
  116. console.log(user);
  117. // return the information including token as JSON
  118. res.json({
  119. success: true,
  120. message: 'Enjoy your token!',
  121. token: token,
  122. avatar: user.avatar
  123. });
  124. }
  125. }
  126. });
  127. };