2022-04-01 10:29:20 +00:00
|
|
|
import mongoose from "mongoose";
|
|
|
|
import validator from "validator";
|
|
|
|
import bcrypt from "bcrypt";
|
2022-04-01 16:16:24 +00:00
|
|
|
import modelNameRegister from "./modelNameRegister.js";
|
2022-04-01 10:29:20 +00:00
|
|
|
|
|
|
|
const Types = mongoose.Schema.Types;
|
|
|
|
|
|
|
|
const userSchema = new mongoose.Schema({
|
|
|
|
email: {
|
2022-04-01 15:27:58 +00:00
|
|
|
type: String,
|
|
|
|
required: true,
|
|
|
|
minlength: 1,
|
|
|
|
trim: true,
|
|
|
|
unique: true,
|
|
|
|
validate: {
|
|
|
|
validator: validator.isEmail,
|
|
|
|
message: "String not email.",
|
|
|
|
}
|
2022-04-01 10:29:20 +00:00
|
|
|
},
|
2022-04-01 15:27:58 +00:00
|
|
|
firstName: { type: String, required: true, trim: true },
|
|
|
|
lastName: { type: String, required: true, trim: true },
|
2022-04-01 10:29:20 +00:00
|
|
|
joinDate: { type: Date, default: Date.now, required: true },
|
2022-04-01 15:27:58 +00:00
|
|
|
phone: { type: Number, required: false, min: 0 },
|
2022-04-01 10:29:20 +00:00
|
|
|
password: {
|
|
|
|
type: String,
|
|
|
|
required: true,
|
|
|
|
minlength: 8
|
|
|
|
// TODO: Custom validator for password requirements?
|
|
|
|
},
|
2022-04-01 16:16:24 +00:00
|
|
|
createdMatches: { type: [{ type: Types.ObjectId, ref: modelNameRegister.Match }], required: true, default: [] },
|
|
|
|
participatingMatches: { type: [{ type: Types.ObjectId, ref: modelNameRegister.Match }], required: true, default: [] },
|
2022-04-01 15:27:58 +00:00
|
|
|
emailPublicity: { type: Number, required: true, default: 0 },
|
|
|
|
bioPublicity: { type: Boolean, required: true, default: false },
|
|
|
|
phonePublicity: { type: Boolean, required: true, default: false },
|
|
|
|
participatingMatchesPublicity: { type: Boolean, required: true, default: false },
|
2022-04-01 16:16:24 +00:00
|
|
|
friends: { type: Types.ObjectId, ref: modelNameRegister.User },
|
2022-04-01 15:27:58 +00:00
|
|
|
accessLevel: { type: Number, required: true, default: 0 },
|
2022-04-01 10:29:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
userSchema.statics.credentialsExist = async function (email, password) {
|
|
|
|
let userModel = this;
|
2022-04-01 15:27:58 +00:00
|
|
|
let user = await userModel.findOne({ email: email });
|
2022-04-01 10:29:20 +00:00
|
|
|
if (!user) {
|
|
|
|
return Promise.reject(new Error("Credentials do not exist."));
|
|
|
|
}
|
|
|
|
if (await bcrypt.compare(password, user.password)) {
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
userSchema.pre("save", function (next) {
|
|
|
|
let user = this;
|
|
|
|
if (user.isModified("password")) { // Only perform hashing if the password has changed.
|
|
|
|
bcrypt.genSalt(10, (err, salt) => {
|
|
|
|
bcrypt.hash(user.password, salt, (err, hash) => {
|
|
|
|
if (err) {
|
|
|
|
throw err; // Probably not, but I'm gonna leave this here for now.
|
|
|
|
}
|
|
|
|
user.password = hash;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-04-01 16:16:24 +00:00
|
|
|
export default mongoose.model(modelNameRegister.User, userSchema);
|