Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}, (err, user) => {
if (err)
throw err;
if(!user){
res.json({
status: false,
message: 'Authentication failed, user not found.'
});
}else{
bcrypt.compare(password, user.password).then((result) => {
if (!result){
res.json({
status: false,
message: 'Authentication failed, wrong password.'
});
}else{
const payload = {
username
};
const token = jwt.sign(payload, req.app.get('api_secret_key'), {
expiresIn: 720 // 12 saat
});
res.json({
status: true,
token
// pick the first found
var user = items[0];
ldapClient.bind(user.dn, password, function (error) {
if (error) return callback('Invalid credentials');
callback(null, { username: username });
});
});
});
});
} else {
var users = safe.JSON.parse(safe.fs.readFileSync(LOCAL_AUTH_FILE));
if (!users || !users[username]) return callback('Invalid credentials');
bcrypt.compare(password, users[username].passwordHash, function (error, valid) {
if (error || !valid) return callback('Invalid credentials');
callback(null, { username: username });
});
}
}
router.delete('/:userId', async (req, res) => {
const validToken = await bcrypt.compare(req.body.token, req.session.token)
if (!validToken) throw abort(401, 'm0BEbqfntUmuU4z3hkeO9A')
const { userId: id } = req.params
if (req.session.user !== id) throw abort(403, 'fmXJKKVWKUaB4rJSQVjkZw')
const user = await anonymizeUser(id)
res.json({ user: omit(user, ['password']) })
})
async check (value, hash) {
return Bcrypt.compare(value, hash)
}
}
resolve: async (parent, { data }, { userService, logger }) => {
logger.info('UserQuery#login.call', data)
const user = await userService.findOne({
where: {
email: data.email
}
})
logger.info('UserQuery#login.check1', isEmpty(user))
if (isEmpty(user)) {
return errorUtils.buildError(['Unable to login'])
}
const isMatch = await bcrypt.compare(data.password, user.password)
logger.info('UserQuery#login.check2', !isMatch)
if (!isMatch) {
return errorUtils.buildError(['Unable to login'])
}
delete user.password
logger.info('UserQuery#login.result', user)
return {
user,
token: await authUtils.generateToken(user.id)
}
}
}, function(err, userDoc) {
if (err) return new Error('auth db error');
if (userDoc) {
bcryptjs.compare(password, userDoc.password, function(err, res) {
if (res) done(err, userDoc);
if (!res) done(err, false, 'Sorry, that password is not right.');
});
} else {
done(err, false, 'Sorry, could not find an account with that username.');
}
});
}
module.exports.comparePassword = function(candidatePassword, hash, callback){
bcrypt.compare(candidatePassword, hash, function(err, isMatch){
if(err){
throw err;
}
callback(null, isMatch);
});
};
FormideClient.db.User.findOne({ email: email }, function (err, user) {
if (err) return next(err);
if (!user) return next(null, false, { message: "Incorrect credentials" });
bcrypt.compare(password, user.password, function(err, isMatch) {
if(err) return next(err);
next(null, isMatch ? user : null)
});
});
}));
async login(data: LoginInput): Promise {
const user = await this.userRepository.findByEmail(data.email)
if (!user) throw new Error("Incorrect email or password")
const isValidPassword = await bcrypt.compare(data.password, user.password)
if (!isValidPassword) throw new Error("Incorrect email or password")
return user
}