const mongoose = require('mongoose'); const validator = require('validator'); const bcrypt = require('bcryptjs'); const jwt = require('jsonwebtoken'); const userSchema = new mongoose.Schema({ username: { type: String, required: true }, password: { type: String, required: true, select : false }, email: { type: String, required: true }, isGoogleAccount: { type: Boolean }, createdAt: { type: Date, default: Date.now } }); userSchema.pre('save', async function (next) { if(!this.isModified('password')) { next(); } this.password = await bcrypt.hash(this.password, 10); }); userSchema.methods.comparePassword = async function (enteredPassword) { return await bcrypt.compare(enteredPassword, this.password); }; userSchema.methods.getJwtToken = function () { return jwt.sign({ id: this._id }, process.env.JWT_SECRET, { expiresIn: process.env.JWT_EXPIRES_TIME }); }; module.exports = mongoose.model('user', userSchema);