arnaucube
arnaucube/commonroutesServerPublic Self-hosted repository
resetPassword script
arnaucode arnaucode@gmail.com
Tue, 20 Feb 2018 22:00:35 +0100
commit

bd0d8494489d308a340748526599b03f7e42e2c9

parent

2c87f5b1b7aa849880543a891f02627f445b73e1

3 files changed, 96 insertions(+), 7 deletions(-)

jump to
M README.md → README.md

@@ -47,13 +47,12 @@ - https (tls/ssl)

``` ### 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:
M controllers/userController.js → controllers/userController.js

@@ -319,7 +319,7 @@ });

}; 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) {
A 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); + }); + } + + });