Change node bcrypt package from bcrypt-nodejs to bcrypt. Change response message on invalid username/pass and changed to bcrypt syntax for compare and hash.
This commit is contained in:
@@ -15,7 +15,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^0.18.0",
|
"axios": "^0.18.0",
|
||||||
"bcrypt-nodejs": "^0.0.3",
|
"bcrypt": "^3.0.6",
|
||||||
"body-parser": "~1.18.2",
|
"body-parser": "~1.18.2",
|
||||||
"cross-env": "~5.1.4",
|
"cross-env": "~5.1.4",
|
||||||
"express": "~4.16.0",
|
"express": "~4.16.0",
|
||||||
|
|||||||
@@ -1,73 +1,72 @@
|
|||||||
const bcrypt = require('bcrypt-nodejs');
|
const bcrypt = require('bcrypt');
|
||||||
const UserRepository = require('src/user/userRepository');
|
const UserRepository = require('src/user/userRepository');
|
||||||
|
|
||||||
class UserSecurity {
|
class UserSecurity {
|
||||||
constructor(database) {
|
constructor(database) {
|
||||||
this.userRepository = new UserRepository(database);
|
this.userRepository = new UserRepository(database);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new user in PlanFlix.
|
* Create a new user in PlanFlix.
|
||||||
* @param {User} user the new user you want to create
|
* @param {User} user the new user you want to create
|
||||||
* @param {String} clearPassword a password of the user
|
* @param {String} clearPassword a password of the user
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
createNewUser(user, clearPassword) {
|
createNewUser(user, clearPassword) {
|
||||||
if (user.username.trim() === '') {
|
if (user.username.trim() === '') {
|
||||||
throw new Error('The username is empty.');
|
throw new Error('The username is empty.');
|
||||||
} else if (clearPassword.trim() === '') {
|
} else if (clearPassword.trim() === '') {
|
||||||
throw new Error('The password is empty.');
|
throw new Error('The password is empty.');
|
||||||
} else {
|
} else {
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
.then(() => this.userRepository.create(user))
|
.then(() => this.userRepository.create(user))
|
||||||
.then(() => UserSecurity.hashPassword(clearPassword))
|
.then(() => UserSecurity.hashPassword(clearPassword))
|
||||||
.then(hash => this.userRepository.changePassword(user, hash));
|
.then(hash => this.userRepository.changePassword(user, hash));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Login into PlanFlix.
|
* Login into PlanFlix.
|
||||||
* @param {User} user the user you want to login
|
* @param {User} user the user you want to login
|
||||||
* @param {String} clearPassword the user's password
|
* @param {String} clearPassword the user's password
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
login(user, clearPassword) {
|
login(user, clearPassword) {
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
.then(() => this.userRepository.retrieveHash(user))
|
.then(() => this.userRepository.retrieveHash(user))
|
||||||
.then(hash => UserSecurity.compareHashes(hash, clearPassword))
|
.then(hash => UserSecurity.compareHashes(hash, clearPassword))
|
||||||
.catch(() => { throw new Error('Wrong username or password.'); });
|
.catch(() => { throw new Error('Incorrect username or password.'); });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compare between a password and a hash password from database.
|
* Compare between a password and a hash password from database.
|
||||||
* @param {String} hash the hash password from database
|
* @param {String} hash the hash password from database
|
||||||
* @param {String} clearPassword the user's password
|
* @param {String} clearPassword the user's password
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
static compareHashes(hash, clearPassword) {
|
static compareHashes(hash, clearPassword) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
bcrypt.compare(clearPassword, hash, (error, matches) => {
|
bcrypt.compare(clearPassword, hash, (error, match) => {
|
||||||
if (matches === true) {
|
if (match)
|
||||||
resolve();
|
resolve()
|
||||||
} else {
|
reject()
|
||||||
reject();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hashes a password.
|
* Hashes a password.
|
||||||
* @param {String} clearPassword the user's password
|
* @param {String} clearPassword the user's password
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
static hashPassword(clearPassword) {
|
static hashPassword(clearPassword) {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
bcrypt.hash(clearPassword, null, null, (error, hash) => {
|
const salatRounds = 10;
|
||||||
resolve(hash);
|
bcrypt.hash(clearPassword, saltRounds, (error, hash) => {
|
||||||
});
|
resolve(hash);
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = UserSecurity;
|
module.exports = UserSecurity;
|
||||||
|
|||||||
Reference in New Issue
Block a user