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.

117 lines
4.5 KiB

  1. var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
  2. var express = require("express");
  3. var app = express();
  4. var config = require('../config'); // get our config file
  5. app.set('superSecret', config.secret); // secret variable
  6. var crypto = require('crypto');
  7. var mongoose = require('mongoose');
  8. var userModel = mongoose.model('userModel');
  9. var pageSize = config.pageSize;
  10. exports.signup = function (req, res) {
  11. var user = new userModel({
  12. username: req.body.username,
  13. password: crypto.createHash('sha256').update(req.body.password).digest('base64'),
  14. shortDescription: req.body.shortDescription,
  15. description: req.body.description,
  16. email: req.body.email
  17. });
  18. user.save(function (err, user) {
  19. if (err) return res.status(500).send(err.message);
  20. //res.status(200).jsonp(user); en comptes de retoranr la data del signup, fem el login directament
  21. console.log("signup fet correctament, redirigint al login internament automàtic");
  22. exports.login(req, res);
  23. });
  24. };
  25. exports.login = function (req, res) {
  26. userModel.findOne({
  27. email: req.body.email
  28. })
  29. .select('+password')
  30. .exec(function (err, user) {
  31. if (err) throw err;
  32. if (!user) {
  33. res.json({success: false, message: 'Authentication failed. User not found.'});
  34. } else if (user) {
  35. req.body.password = crypto.createHash('sha256').update(req.body.password).digest('base64');
  36. if (user.password != req.body.password) {
  37. res.json({success: false, message: 'Authentication failed. Wrong password.'});
  38. } else {
  39. var indexToken = -1;
  40. for (var i = 0; i < user.tokens.length; i++) {
  41. if (user.tokens[i].userAgent == req.body.userAgent) {
  42. indexToken = JSON.parse(JSON.stringify(i));//stringify i parse pq es faci una còpia de la variable i, enlloc de una referència
  43. }
  44. }
  45. console.log(indexToken);
  46. if (indexToken == -1) {//userAgent no exist
  47. var tokenGenerated = jwt.sign({foo: 'bar'}, app.get('superSecret'), {
  48. // expiresIn: 86400 // expires in 24 hours
  49. });
  50. var newToken = {
  51. userAgent: req.body.userAgent,
  52. token: tokenGenerated,
  53. os: req.body.os,
  54. browser: req.body.browser,
  55. device: req.body.device,
  56. os_version: req.body.os_version,
  57. browser_version: req.body.browser_version,
  58. ip: req.body.ip,
  59. lastLogin: Date()
  60. };
  61. user.tokens.push(newToken);
  62. } else {//userAgent already exist
  63. user.tokens[indexToken].token = "";
  64. var tokenGenerated = jwt.sign({foo: 'bar'}, app.get('superSecret'), {
  65. // expiresIn: 86400 // expires in 24 hours
  66. });
  67. user.tokens[indexToken].token = tokenGenerated;
  68. user.tokens[indexToken].ip = req.body.ip;
  69. user.tokens[indexToken].lastLogin = Date();
  70. }
  71. user.save(function (err, user) {
  72. if (err) return res.send(500, err.message);
  73. // return the information including token as JSON
  74. user.password = "";
  75. res.json({
  76. user: user,
  77. success: true,
  78. message: 'Enjoy your token!',
  79. token: tokenGenerated
  80. });
  81. });
  82. }
  83. }
  84. });
  85. };
  86. exports.getAllUsers = function(req, res) {
  87. userModel.find()
  88. .limit(Number(req.query.pageSize))
  89. .skip(pageSize * Number(req.query.page))
  90. .exec(function (err, users) {
  91. if (err) return res.send(500, err.message);
  92. res.status(200).jsonp(users);
  93. });
  94. };
  95. exports.getUserById = function (req, res) {
  96. userModel.findOne({_id: req.params.userid})
  97. .lean()
  98. .populate('events', 'title shortDescription description img date')
  99. .exec(function (err, user) {
  100. if (err) return res.send(500, err.message);
  101. if (!user) {
  102. res.json({success: false, message: 'User not found.'});
  103. } else if (user) {
  104. res.status(200).jsonp(user);
  105. }
  106. });
  107. };