From bd0d8494489d308a340748526599b03f7e42e2c9 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Tue, 20 Feb 2018 22:00:35 +0100 Subject: [PATCH] resetPassword script --- README.md | 11 ++--- controllers/userController.js | 2 +- resetPassword.js | 90 +++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 resetPassword.js diff --git a/README.md b/README.md index 97c217c..2bf63d9 100755 --- a/README.md +++ b/README.md @@ -47,13 +47,12 @@ code: https://github.com/arnaucode/commonroutesApp ``` ### Configuration before run: -In the file adminConfig.js, put the sha256 of the password that allows to create new admins: -```js -module.exports = { - 'passwordHash': 'Bzij4hEeEUpmXTWyS+X0LR+YcA8WFjP2P7qhW0sxA6s='/*password raw: adminPassword*/ -}; +In controllers/userController.js, define the port of the goImgServer: +``` +function postImage(req, res, filename, fileImg) { + url = "http://127.0.0.1:3001/image"; + [...] ``` - #### RESOURCES using: diff --git a/controllers/userController.js b/controllers/userController.js index 6aa7425..4aa85db 100755 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -319,7 +319,7 @@ exports.getNotifications = function(req, res) { }; function postImage(req, res, filename, fileImg) { - url = "http://127.0.0.1:3050/image"; + url = "http://127.0.0.1:3001/image"; var importFile = function(fileImg) { var decodedFile = new Buffer(fileImg, 'base64'); var r = request.post(url, function(err, httpResponse, body) { diff --git a/resetPassword.js b/resetPassword.js new file mode 100644 index 0000000..94ab953 --- /dev/null +++ b/resetPassword.js @@ -0,0 +1,90 @@ +/* +script to reset password for a user +*/ + +var config = require('./config'); +var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens +var crypto = require('crypto'); + +var mongoose = require('mongoose'); + +mongoose.Promise = global.Promise; +// Connection to DB +mongoose.connect(config.database, function(err, res) { + if (err) { + console.log(err); + }; + console.log('Connected to Database'); +}); + +var userMdl = require('./models/userModel'); +var userModel = mongoose.model('userModel'); + +var readlineSync = require('readline-sync'); + + + +console.log("Welcome to Common Routes"); +console.log("----------"); +console.log("This is the resetPassword.js"); + + +var username = readlineSync.question('Enter the username: '); +if ((username == "")) { + console.log("username can't be empty"); + process.exit(0); +} + +console.log('Hi ' + username + '!'); + + +var clearPassword = readlineSync.question('Enter the new password: ', { + hideEchoBack: true // The typed text on screen is hidden by `*` (default). +}); +var clearPassword2 = readlineSync.question('Enter the new password again: ', { + hideEchoBack: true // The typed text on screen is hidden by `*` (default). +}); +if (clearPassword != clearPassword2) { + console.log("passwords don't match"); + process.exit(0); +} +if (clearPassword == "undefined") { + console.log("Password can't be empty"); + process.exit(0); +} +/*if (clearPassword.length < 10) { + console.log("Please, choose a password with more than 10 characters"); + process.exit(0); +}*/ + + +var newPassword = crypto.createHash('sha256').update(clearPassword).digest('base64') +console.log(newPassword); +userModel.findOne({ + username: username + }) + .select('+password') + .exec(function(err, user) { + if (err) { + console.log(err); + process.exit(0); + } + console.log(user); + + if (!user) { + console.log("user not found"); + process.exit(0); + } else if (user) { + user.password = newPassword; + user.save(function(err, user) { + if (err) { + console.log(err); + process.exit(0); + } + + console.log("password successfully changed"); + process.exit(0); + }); + } + + });