created migration to brypt existing passwords
This commit is contained in:
parent
436067150e
commit
288a19ea97
1 changed files with 46 additions and 0 deletions
46
migrations/UpdateUserPasswords5.js
Normal file
46
migrations/UpdateUserPasswords5.js
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
const db = require('../models');
|
||||||
|
const bcrypt = require('bcrypt');
|
||||||
|
const logger = require('winston');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
up: (queryInterface, Sequelize) => {
|
||||||
|
// get all the users
|
||||||
|
return db.User
|
||||||
|
.findAll()
|
||||||
|
.then((users) => {
|
||||||
|
// create an array of promises, with each promise bcrypting a password and updating the record
|
||||||
|
const promises = users.map((record) => {
|
||||||
|
// bcrypt
|
||||||
|
// generate a salt string to use for hashing
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
bcrypt.genSalt((saltError, salt) => {
|
||||||
|
if (saltError) {
|
||||||
|
logger.error('salt error', saltError);
|
||||||
|
reject(saltError);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// generate a hashed version of the user's password
|
||||||
|
bcrypt.hash(record.password, salt, (hashError, hash) => {
|
||||||
|
// if there is an error with the hash generation return the error
|
||||||
|
if (hashError) {
|
||||||
|
logger.error('hash error', hashError);
|
||||||
|
reject(hashError);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// replace the password string with the hash password value
|
||||||
|
resolve(queryInterface.sequelize.query(`UPDATE User SET User.password = "${hash}" WHERE User.id = ${record.id}`));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
// return the array of promises
|
||||||
|
return Promise.all(promises);
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
logger.error('error prepping promises array', error);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
down: (queryInterface, Sequelize) => {
|
||||||
|
// logic for reverting the changes
|
||||||
|
},
|
||||||
|
};
|
Loading…
Reference in a new issue