const mongoose = require('mongoose'); const bcrypt = require('bcrypt'); const UserSchema = new mongoose.Schema({ username: { type: String, required: true, unique: true }, password: { type: String, required: true }, following: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }], profilePicture: { type: String, default: "" }, bio: { type: String, default: "" }, pronouns: { type: String, default: "" }, email: { type: String, default: "" }, name: { type: String, default: "" }, }); UserSchema.methods.validPassword = function (password) { return bcrypt.compareSync(password, this.password); }; UserSchema.pre('save', function (next) { const user = this; if (!user.isModified('password')) return next(); bcrypt.genSalt(10, function (err, salt) { if (err) return next(err); bcrypt.hash(user.password, salt, function (err, hash) { if (err) return next(err); user.password = hash; next(); }); }); }); module.exports = mongoose.model('User', UserSchema);