Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return Validation.userExists(username).then(user => {
if (!bcrypt.compareSync(oldPassword, user.password)) {
throw new Error("Incorrect password");
}
const newPasswordHash = bcrypt.hashSync(newPassword, 10);
const updateOperation = {$set: {password: newPasswordHash}};
return mean.db.collection("users")
.updateOne({username: username}, updateOperation)
.then(write_res => {
if (write_res.modifiedCount !== 1) {
throw new Error("Couldn't save new password");
}
// report
return bus.update_atom("User", user.atom_id, updateOperation);
})
static async verifyEmailPassword(email, plainPassword) {
// 查询用户是否存在
const user = await User.findOne({
where: {
email
}
})
if (!user) {
throw new global.errs.AuthFailed('账号不存在')
}
// 验证密码是否正确
const correct = bcrypt.compareSync(plainPassword, user.password);
if (!correct) {
throw new global.errs.AuthFailed('密码不正确')
}
return user
}
return Validation.userExists(username).then(user => {
// TODO: promisify
if (!bcrypt.compareSync(password, user.password)) {
throw new Error("Incorrect password");
}
const token = jwt.sign(username, "ultra-secret-key");
return JSON.stringify({
token: token,
user: user
});
});
}
it('compare plain passwords to encrypted ones', function () {
this.timeout(9000);
assert.isOk(bcrypt.compareSync(usersDb[0].plainPassword, usersDb[0].password), '[0]');
assert.isOk(bcrypt.compareSync(usersDb[1].plainPassword, usersDb[1].password), '[1]');
assert.isOk(bcrypt.compareSync(usersDb[0].plainNewPassword, usersDb[0].newPassword), 'new [0]');
assert.isOk(bcrypt.compareSync(usersDb[1].plainNewPassword, usersDb[1].newPassword), 'new [1]');
});
});
global.galleon.changePassword("hash@example.com", "changetopass", "okpassword", function(error, user) {
if(error) throw error;
expect(user.email).to.equal("hash@example.com");
expect(bcrypt.compareSync("changetopass", user.password)).to.equal(true);
done();
})
})
userSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.password);
};
}).then(result => {
const Item = result.Items[0];
if (!Item) return Promise.reject('User not found');
let match = bcryptjs.compareSync(password, Item.password);
if (!match) return Promise.reject('invalid password');
delete Item.password;
Item.jwt = authenticate(Item);
Item.gravatar = gravatar.url(Item.email, {s: '100', r: 'x', d: 'retro'}, true);
return Item;
});
},
ChatServer.prototype.isRoomPasswordValid = function (password, hash) {
var bcrypt = require('bcryptjs');
return bcrypt.compareSync(password, hash);
};
static checkPassword(password, hash) {
return bcrypt.compareSync(password, hash);
}
}
authenticate(plainTextPassword: string) {
return bcrypt.compareSync(plainTextPassword, this.password);
},
encryptPassword(password: string) {