Created
December 10, 2019 17:16
-
-
Save vbguard/79d90aa7f268fd7242565e468e13d6cc to your computer and use it in GitHub Desktop.
MongoDB user model methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
userSchema.virtual('tasks', { | |
ref: 'Task', | |
localField: '_id', | |
foreignField: 'owner' | |
}) | |
userSchema.methods.toJSON = function () { | |
const user = this | |
const userObject = user.toObject() | |
delete userObject.password | |
delete userObject.tokens | |
delete userObject.avatar | |
return userObject | |
} | |
userSchema.methods.generateAuthToken = async function () { | |
const user = this | |
const token = jwt.sign({ _id: user._id.toString() }, process.env.JWT_SECRET) | |
user.tokens = user.tokens.concat({ token }) | |
await user.save() | |
return token | |
} | |
userSchema.statics.findByCredentials = async (email, password) => { | |
const user = await User.findOne({ email }) | |
if (!user) { | |
throw new Error('Unable to login') | |
} | |
const isMatch = await bcrypt.compare(password, user.password) | |
if (!isMatch) { | |
throw new Error('Unable to login') | |
} | |
return user | |
} | |
// Hash the plain text password before saving | |
userSchema.pre('save', async function (next) { | |
const user = this | |
if (user.isModified('password')) { | |
user.password = await bcrypt.hash(user.password, 8) | |
} | |
next() | |
}) | |
// Delete user tasks when user is removed | |
userSchema.pre('remove', async function (next) { | |
const user = this | |
await Task.deleteMany({ owner: user._id }) | |
next() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment