const mongoose = require('mongoose'); //pour vérifier la validité d'une adresse mail const { isEmail } = require('validator'); const bcrypt = require('bcrypt'); //trim pour supprimer les espaces const userSchema = new mongoose.Schema( { pseudo: { type: String, required: true, minLength: 3, maxLength: 55, unique: true, trim: true }, email: { type: String, required: true, validate: [isEmail], lowercase: true, unique: true, trim: true }, password: { type: String, required: true, max: 1024, minlength: 6 }, picture: { type: String, default: "/random-user.png" }, bio :{ type: String, max: 1024, }, followers: { type: [String] }, following: { type: [String] }, likes: { type: [String] } }, { timestamps: true, } ); //timestamps pour savoir quand l'utilisateur c'est connecté // play function before save into display: 'block', userSchema.pre("save", async function(next) { const salt = await bcrypt.genSalt(); this.password = await bcrypt.hash(this.password, salt); next(); }); userSchema.statics.login = async function({email, password}) { const user = await this.findOne( {email} ); console.log(user); if (user) { const auth = await bcrypt.compare(password, user.password); if (auth) { return user; } throw Error('incorrect password'); } throw Error('incorrect email') }; const UserModel = mongoose.model("user", userSchema); module.exports = UserModel;