const mongoose = require("mongoose"); const bcrypt = require('bcryptjs'); const userSchema = new mongoose.Schema({ email: { type: String, trim: true, lowercase: true, unique: true, required: true, min: 4, max: 255, validate(value) { if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) { throw new Error('Wrong email address'); } } }, password: { type: String, required: true, min: 4, max: 1024, }, language: { type: String, required: true, min: 2, max: 2, } }); 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; } userSchema.pre('save', async function(next){ const user = this; if (user.isModified('password')) { user.password = await bcrypt.hash(user.password, 8); } next(); }) const User = mongoose.model('User', userSchema); module.exports = User;