Skip to content

Instantly share code, notes, and snippets.

@chewtoys
Created July 29, 2019 20:20
Show Gist options
  • Save chewtoys/edc426f22404fc78337941cc0252ec80 to your computer and use it in GitHub Desktop.
Save chewtoys/edc426f22404fc78337941cc0252ec80 to your computer and use it in GitHub Desktop.
/**
* Find user by email
*/
async findUser(options) {
const {
email,
password,
} = options
if (!email) {
throw new Error({
message: 'An email is required',
})
}
const user = await this.findOne({
email,
}).exec()
const err = {
status: httpStatus.UNAUTHORIZED,
isPublic: true,
}
if (password) {
if (user && await user.passwordMatches(password)) {
return {
user
}
}
err.message = 'Incorrect email or password'
} else {
err.message = 'Incorrect email'
}
throw new Error(err)
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment