-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathuser.js
139 lines (125 loc) · 3.14 KB
/
user.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
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
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');
var winston = require('../config/winston');
var UserSchema = new Schema({
_id: Schema.Types.ObjectId,
email: {
type: String,
unique: true,//remove unique on db
required: true,
index: true
},
password: {
type: String,
required: true,
// https://stackoverflow.com/questions/12096262/how-to-protect-the-password-field-in-mongoose-mongodb-so-it-wont-return-in-a-qu
select: false
},
firstname: {
type: String,
// required: true
},
lastname: {
type: String,
// required: true
},
emailverified: {
type: Boolean,
// required: true
},
resetpswrequestid: {
type: String,
select: false
},
signedInAt: {
type: Date
},
// db.users.find({authUrl: {$exists : false }}).forEach(function(mydoc) {
// db.users.update({_id: mydoc._id}, {$set: {authUrl: Math.random().toString(36).substring(2) + Date.now().toString(36)}})
// })
authUrl: {
type: String,
index: true
},
attributes: {
type: Object,
},
status: {
type: Number,
required: true,
default: 100,
index: true,
// select: false
},
description: {
type: String,
},
public_email: {
type: String,
required: false
},
public_website: {
type: String,
required: false
}
// authType: { // update db old data
// type: String,
// index:true,
// default: 'email_password'
// },
// auth: {
// type: Schema.Types.ObjectId,
// ref: 'auth',
// //required: true
// },
}, {
timestamps: true
}
);
// UserSchema.set('toJSON', {
// transform: function(doc, ret, opt) {
// delete ret['password']
// return ret
// }
// });
UserSchema.pre('save', function (next) {
var user = this;
if (this.isModified('password') || this.isNew) {
bcrypt.genSalt(10, function (err, salt) {
if (err) {
return next(err);
}
bcrypt.hash(user.password, salt, null, function (err, hash) {
if (err) {
return next(err);
}
user.password = hash;
next();
});
});
} else {
return next();
}
});
UserSchema.methods.comparePassword = function (passw, cb) {
bcrypt.compare(passw, this.password, function (err, isMatch) {
if (err) {
return cb(err);
}
cb(null, isMatch);
});
};
UserSchema.virtual('fullName').get(function () {
return (this.firstname || '') + ' ' + (this.lastname || '');
});
//UserSchema.index({ email: 1, authType: 1 }, { unique: true });
var UserModel = mongoose.model('user', UserSchema);
// UserModel.getFullname = function () {
// return
// };
if (process.env.MONGOOSE_SYNCINDEX) {
UserModel.syncIndexes();
winston.verbose("UserModel syncIndexes")
}
module.exports = UserModel;