-
Notifications
You must be signed in to change notification settings - Fork 1
/
UserModel.js
60 lines (55 loc) · 1.36 KB
/
UserModel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const mongoose = require("mongoose");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
const { isEmail } = require("validator");
require("dotenv").config();
const UserSchema = new mongoose.Schema(
{
firstname: {
type: String,
required: [true, "Please input your firstname"],
},
lastname: {
type: String,
required: [true, "Please input your lastname"],
},
email: {
type: String,
unique: true,
validate: [isEmail, "Please input a valid Email"],
},
password: {
type: String,
required: ["Please input this field"],
},
children: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "child",
},
],
cards: [{ type: mongoose.Schema.Types.ObjectId, ref: "card" }],
},
{ timeStamps: true }
);
// UserSchema.pre("save",async function(next){
// const salt = await bcrypt.genSalt(10)
// const hash = await bcrypt.hash(this.password, salt)
// this.password = hash
// next()
// })
UserSchema.methods.generateJWT = function () {
const token = jwt.sign(
{
_id: this._id,
firstname: this.firstname,
lastname: this.lastname,
email: this.email,
},
process.env.JWT_SECRET_KEY,
{ expiresIn: "1d" }
);
return token;
};
const UserModel = mongoose.model("user", UserSchema);
module.exports = UserModel;