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.

139 lines
3.5 KiB

  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. console.log('GET /users');
  16. res.status(200).jsonp(users);
  17. });
  18. };
  19. //GET - Return a User with specified ID
  20. exports.findById = function(req, res) {
  21. userModel.findById(req.params.id, function(err, user) {
  22. if(err) return res.send(500, err.message);
  23. console.log('GET /users/' + req.params.id);
  24. res.status(200).jsonp(user);
  25. });
  26. };
  27. exports.findUserByUsername = function(req, res) {
  28. userModel.find({
  29. username: req.params.username
  30. }, function(err, user) {
  31. if (err) throw err;
  32. if (!user) {
  33. res.json({ success: false, message: 'no user found' });
  34. } else if (user) {
  35. console.log(user);
  36. // return the information including token as JSON
  37. res.jsonp(user);
  38. }
  39. });
  40. };
  41. //POST - Insert a new User in the DB
  42. exports.addUser = function(req, res) {
  43. console.log('POST new user, name: ' + req.body.username);
  44. //console.log(req.body);
  45. var user = new userModel({
  46. username: req.body.username,
  47. password: req.body.password,
  48. description: req.body.description,
  49. avatar: req.body.avatar,
  50. mail: req.body.mail,
  51. admin: req.body.admin
  52. });
  53. user.save(function(err, user) {
  54. if(err) return res.send(500, err.message);
  55. res.status(200).jsonp(user);
  56. });
  57. };
  58. //PUT - Update a user already exists
  59. exports.updateUser = function(req, res) {
  60. userModel.findById(req.params.id, function(err, user) {
  61. user.username = req.body.username;
  62. user.password = req.body.password;
  63. user.description = req.body.description;
  64. user.avatar = req.body.avatar;
  65. user.mail = req.body.mail;
  66. user.admin = req.body.admin;
  67. user.save(function(err) {
  68. if(err) return res.send(500, err.message);
  69. res.status(200).jsonp(user);
  70. });
  71. });
  72. };
  73. //DELETE - Delete a user with specified ID
  74. exports.deleteUser = function(req, res) {
  75. userModel.findById(req.params.id, function(err, user) {
  76. user.remove(function(err) {
  77. if(err) return res.send(500, err.message);
  78. res.status(200).jsonp(req.params.id);
  79. console.log('DELETE /users/' + req.params.id);
  80. })
  81. });
  82. };
  83. //POST - auth user
  84. exports.login = function(req, res) {
  85. // find the user
  86. userModel.findOne({
  87. username: req.body.username
  88. }, function(err, user) {
  89. if (err) throw err;
  90. if (!user) {
  91. res.json({ success: false, message: 'Authentication failed. User not found.' });
  92. } else if (user) {
  93. // check if password matches
  94. if (user.password != req.body.password) {
  95. res.json({ success: false, message: 'Authentication failed. Wrong password.' });
  96. } else {
  97. // if user is found and password is right
  98. // create a token
  99. var token = jwt.sign(user, app.get('superSecret'), {
  100. //expiresInMinutes: 1440 // expires in 24 hours
  101. expiresIn: '60m'
  102. });
  103. console.log(user);
  104. // return the information including token as JSON
  105. res.json({
  106. success: true,
  107. message: 'Enjoy your token!',
  108. token: token,
  109. avatar: user.avatar
  110. });
  111. }
  112. }
  113. });
  114. };