Skip to content

Instantly share code, notes, and snippets.

@Cool-sami12
Created November 21, 2020 00:05
Show Gist options
  • Select an option

  • Save Cool-sami12/eaa398a4bdb46aca3c6ddd50cf47946f to your computer and use it in GitHub Desktop.

Select an option

Save Cool-sami12/eaa398a4bdb46aca3c6ddd50cf47946f to your computer and use it in GitHub Desktop.
question about static schema in a model
// // find by token
// userSchema.statics.findByToken = function(token,cb){
// var user = this;
// var SECRET= process.env.SECRET;
// jwt.verify(token,SECRET,function(err,decode){
// user.findone({"_id": decode, "token":token},function(err,user){
// if(err) return cb(err);
// cb(null,user);
// })
// })
// };
userSchema.methods.generateAuthToken = async function() {
// Generate an auth token for the user
const user = this
const token = jwt.sign({_id: user._id}, process.env.SECRET)
user.tokens = user.tokens.concat({token})
await user.save()
return token
}
// userSchema.statics.findByCredentials = async (email, password) => {
// // Search for a user by email and password.
// const user = await User.findOne({ email} )
// if (!user) {
// throw new Error({ error: 'Invalid login credentials' })
// }
// const isPasswordMatch = await bcrypt.compare(password, user.password)
// if (!isPasswordMatch) {
// throw new Error({ error: 'Invalid login credentials' })
// }
// return user
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment