Skip to content

Instantly share code, notes, and snippets.

@adnanrahic
Created April 26, 2018 12:56
/**
* Helpers
*/
function login(eventBody) {
return User.findOne({ email: eventBody.email })
.then(user =>
!user
? Promise.reject(new Error('User with that email does not exits.'))
: comparePassword(eventBody.password, user.password, user._id)
)
.then(token => ({ auth: true, token: token }));
}
function comparePassword(eventPassword, userPassword, userId) {
return bcrypt.compare(eventPassword, userPassword)
.then(passwordIsValid =>
!passwordIsValid
? Promise.reject(new Error('The credentials do not match.'))
: signToken(userId)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment