You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
4.5 KiB
112 lines
4.5 KiB
const mongoose = require('mongoose');
|
|
|
|
//pour vérifier la validité d'une adresse mail
|
|
const { isEmail } = require('validator');
|
|
const bcrypt = require('bcrypt');
|
|
|
|
|
|
const NotifSchema = new mongoose.Schema({
|
|
typeNotif: {
|
|
type: String,
|
|
required: true
|
|
},
|
|
id_user: {
|
|
type: String,
|
|
required: true
|
|
},
|
|
id_post1: {
|
|
type: String,
|
|
required: true
|
|
},
|
|
id_post2: {
|
|
type: String,
|
|
required: true
|
|
}
|
|
});
|
|
|
|
const PictureSchema = new mongoose.Schema({
|
|
data: {
|
|
type: String,
|
|
required: true,
|
|
},
|
|
contentType: {
|
|
type: String,
|
|
required: true,
|
|
}
|
|
});
|
|
|
|
|
|
//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: PictureSchema,
|
|
default: { data: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAgVBMVEX///8AAACgoKDd3d38/Pza2tr4+Pjk5OT29vbv7+/Hx8eVlZXAwMCnp6f6+vrh4eFWVlbT09OKiootLS3CwsKAgIAiIiKPj4+urq5ISEjr6+tqampcXFwxMTEWFha2trZzc3M7OzuZmZlHR0cgICATExNRUVGCgoILCwt2dnY9PT16/9JNAAAGjUlEQVR4nO2dh3bqMAyGMRBGKGWWUTYdlL7/A15S4DISEkuWKrnH3wO0+g+JLGulVAoEAoFAIBAIBAKBQC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EP69jiHRqhNTMAAAAAElFTkSuQmCC", contentType: "image/png" }
|
|
},
|
|
bio :{
|
|
type: String,
|
|
max: 1024,
|
|
},
|
|
followers: {
|
|
type: [String]
|
|
},
|
|
following: {
|
|
type: [String]
|
|
},
|
|
likes: {
|
|
type: [String]
|
|
},
|
|
notif: [NotifSchema]
|
|
},
|
|
{
|
|
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} );
|
|
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;
|