-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathuser.js
55 lines (50 loc) · 1.57 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
// load the things we need
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
//TODO: if linked to fb/twitter/google find friends by contacts
// define the schema for our user model
var userSchema = mongoose.Schema({
local: {
email: String,
password: String,
passwordResetToken: String,
passwordResetExpiration: Date
},
facebook: {
id: String,
token: String,
email: String,
name: String
},
twitter: {
id: String,
token: String,
displayName: String,
username: String
},
google: {
id: String,
token: String,
email: String,
name: String
},
activeGames: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Gamestate' }], // external reference to gamestate objects
friends: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
username: { type: String, lowercase: true, trim: true, unique: true, sparse: true },
email_notifications: Boolean,
twitter_notifications: Boolean,
sound_notifications: Boolean,
collapsible_menu: Boolean,
dark_mode: Boolean,
preferred_color: String
});
// generating a hash
userSchema.statics.generateHash = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
// checking if password is valid
userSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.local.password);
};
// create the model for users and expose it to our app
module.exports = mongoose.model('User', userSchema);