mirror of
https://github.com/arnaucube/commonroutesServer.git
synced 2026-02-27 21:16:41 +01:00
resetPassword script
This commit is contained in:
11
README.md
11
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:
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
90
resetPassword.js
Normal file
90
resetPassword.js
Normal file
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user