Skip to content

Instantly share code, notes, and snippets.

@FikriRNurhidayat
Created August 16, 2019 08:54
Show Gist options
  • Save FikriRNurhidayat/ab1fabfd1d1d13f1cc47dbcd2504e2f1 to your computer and use it in GitHub Desktop.
Save FikriRNurhidayat/ab1fabfd1d1d13f1cc47dbcd2504e2f1 to your computer and use it in GitHub Desktop.
Creating authenticate methods on User Model
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const uniqueValidator = require('mongoose-unique-validator');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');
const userSchema = new Schema({
name: {
type: String,
max: 255,
required: true
},
email: {
type: String,
lowercase: true,
required: true,
unique: true
},
password: {
type: String,
required: true
},
isVerified: {
type: Boolean,
required: true,
default: false
}
});
userSchema.plugin(uniqueValidator);
var User = mongoose.model('User', userSchema);
User.authenticate = function(data) {
return new Promise(async function(resolve, reject) {
let user = await User.findOne({
email: data.email
});
if (!user) {
return reject("User doesn't exist!")
}
let isPasswordCorrect = bcrypt.compareSync(data.password, user.password);
if (!isPasswordCorrect) {
return reject("Wrong password!");
}
let token = jwt.sign({
_id: user._id
}, process.env.SECRET_OR_KEY);
return resolve(token);
})
}
module.exports = User;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment