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.

90 lines
2.1 KiB

  1. /*
  2. script to reset password for a user
  3. */
  4. var config = require('./config');
  5. var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
  6. var crypto = require('crypto');
  7. var mongoose = require('mongoose');
  8. mongoose.Promise = global.Promise;
  9. // Connection to DB
  10. mongoose.connect(config.database, function(err, res) {
  11. if (err) {
  12. console.log(err);
  13. };
  14. console.log('Connected to Database');
  15. });
  16. var userMdl = require('./models/userModel');
  17. var userModel = mongoose.model('userModel');
  18. var readlineSync = require('readline-sync');
  19. console.log("Welcome to Common Routes");
  20. console.log("----------");
  21. console.log("This is the resetPassword.js");
  22. var username = readlineSync.question('Enter the username: ');
  23. if ((username == "")) {
  24. console.log("username can't be empty");
  25. process.exit(0);
  26. }
  27. console.log('Hi ' + username + '!');
  28. var clearPassword = readlineSync.question('Enter the new password: ', {
  29. hideEchoBack: true // The typed text on screen is hidden by `*` (default).
  30. });
  31. var clearPassword2 = readlineSync.question('Enter the new password again: ', {
  32. hideEchoBack: true // The typed text on screen is hidden by `*` (default).
  33. });
  34. if (clearPassword != clearPassword2) {
  35. console.log("passwords don't match");
  36. process.exit(0);
  37. }
  38. if (clearPassword == "undefined") {
  39. console.log("Password can't be empty");
  40. process.exit(0);
  41. }
  42. /*if (clearPassword.length < 10) {
  43. console.log("Please, choose a password with more than 10 characters");
  44. process.exit(0);
  45. }*/
  46. var newPassword = crypto.createHash('sha256').update(clearPassword).digest('base64')
  47. console.log(newPassword);
  48. userModel.findOne({
  49. username: username
  50. })
  51. .select('+password')
  52. .exec(function(err, user) {
  53. if (err) {
  54. console.log(err);
  55. process.exit(0);
  56. }
  57. console.log(user);
  58. if (!user) {
  59. console.log("user not found");
  60. process.exit(0);
  61. } else if (user) {
  62. user.password = newPassword;
  63. user.save(function(err, user) {
  64. if (err) {
  65. console.log(err);
  66. process.exit(0);
  67. }
  68. console.log("password successfully changed");
  69. process.exit(0);
  70. });
  71. }
  72. });