user.js
4.32 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
const crypto = require('crypto');
//const Pet = require("./pet");
const UserSchema = new Schema({
//login require
//email == id
email: { type: String, required: true, unique: true },
password: String,
token: String,
//user info
phone: {type: String, require: true},
nickname: {type: String, default: ""},
books: [{type:mongoose.Schema.Types.ObjectId, ref:'book', required:false}],//array of bookid
pages:[{type:mongoose.Schema.Types.ObjectId, ref:'page', required:false}],
favorite: [{ type: String }],//String of url(book, page)
});
function hash(password) {
return crypto.createHmac('sha256', process.env.SECRET_KEY).update(password).digest('hex');
}
UserSchema.statics.localRegister = function ({ nickname,phone, email, password }) {
const user = new this({
phone,
email,
password: hash(password),
nickname
});
return user.save();
};
//User 모델에 정보를 save하기 전에 실행
/*
UserSchema.pre('save', function( next ){
var user = this;
//비밀번호를 바꿀때만 비밀번호를 암호화해준다
if (user.isModified('password')) {
// 비밀번호를 암호화 시킨다
bcrypt.hash(user.password, 10, function (err, hash) { //hash: 암호화된 비밀번호
if (err) return next(err);
user.password = hash();
next();
});
} else {
next();
}
});
*/
// parameters: login user email, new password to change
UserSchema.statics.updatePassword = async function (email, password) {
const hash = await hash(password);
this.findOneAndUpdate({email: email} , {$set: {password: hash}}, function(err, user) {
if(err) throw err;
console.log(user)
})
console.log("hash", hash);
};
UserSchema.methods.generateToken = function () {
const token = jwt.sign(
// 첫번째 파라미터엔 토큰 안에 집어넣고 싶은 데이터를 넣습니다
{
_id: this.id,
email: this.email,
},
process.env.JWT_SECRET, // 두번째 파라미터에는 JWT 암호를 넣습니다
{
expiresIn: "7d", // 7일동안 유효함
}
);
return token;
};
UserSchema.statics.updateFcm = async function (email, fcm){
this.findOneAndUpdate({email: email}, {$set: {fcmToken: fcm}}, function(err,user){
if(err) throw err;
//console.log(user);
})
};
//응답하는 JSON에서 비밀번호 정보 삭제
UserSchema.methods.serialize = async function () {
const data = this.toJSON();
delete data.password;
delete data._id;
return data;
};
UserSchema.methods.checkPassword = async function (password) {
const hashed = hash(password);
return this.password === hashed;
};
//static method
UserSchema.statics.findByEmail = async function (email) {
return this.findOne({ email: email, logintype: "local" });
};
// parameters: user information to be updated
UserSchema.statics.updateUser = async function (user) {
this.findOneAndUpdate({ email: user.email }, { $set: user },
function (err, result) {
if (err) throw err;
return result;
});
};
// parameters: book's object id
UserSchema.methods.addBook = async function (bookId) {
console.log(bookId);
this.books.push(bookId);
this.save();
}
//parameters: login user email, pet's object id
UserSchema.statics.delBook = async function (email, bookId) {
return this.findOneAndUpdate({email: email}, {$pull: {books: bookId}});
}
// parameters: login user email, url of the book or page
UserSchema.statics.updateFavorite = async function (email, contentsID) {
return this.findOneAndUpdate(
{
email: email,
},
{ $addToSet: { favorite: contentsID } }
);
};
// parameters: login user email, url of the book or page
UserSchema.statics.delFavorites = async function (email, contentsID) {
return this.findOneAndUpdate(
{
email: email,
},
{ $pull: { favorite: contentsID } }
);
};
UserSchema.statics.showFavorites = async function (email) {
this.books
return (
{
email: email,
},
{ $pull: { favorite: contentsID } }
);
};
module.exports = mongoose.model("User", UserSchema, "Users");