-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
80 lines (72 loc) · 1.95 KB
/
db.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
/*
@author: Zachary Kimelheim
netID: zk377
*/
const mongoose = require('mongoose')
const bcrypt = require('bcryptjs')
const UserSchema = new mongoose.Schema({
first: String,
last: String,
username: String,
email: String,
password: String,
Restaurants: {type: Array, 'default': []},
Plans: {type: Array, 'default': []},
// google: {
// id: String,
// token: String,
// email: String,
// name: String,
// }
// facebook data if logged in with facebook
fbid: String,
fbtoken: String,
fbemail: String,
fbname: String
})
const Restaurant = new mongoose.Schema({
userID: String,
name: String,
address: String,
latitude: Number,
longitude: Number,
cuisinetype: String,
price: String
})
const LunchPlan = new mongoose.Schema({
userID: String,
monday: String,
tuesday: String,
wednesday: String,
thursday: String,
friday: String
})
mongoose.model('User', UserSchema)
mongoose.model('Restaurant', Restaurant)
mongoose.model('LunchPlan', LunchPlan)
const User = module.exports = mongoose.model('User', UserSchema)
// Passport functions used for authentication
module.exports.createUser = function (newUser, callback) {
bcrypt.genSalt(10, function (err, salt) {
bcrypt.hash(newUser.password, salt, function (err, hash) {
newUser.password = hash
newUser.save(callback)
})
})
}
module.exports.getUserByUsername = function (username, callback) {
const query = {username: username}
User.findOne(query, callback)
}
module.exports.getUserByID = function (id, callback) {
User.findById(id, callback)
}
module.exports.comparePassword = function (candidatePassword, hash, callback) {
bcrypt.compare(candidatePassword, hash, function (err, isMatch) {
if (err) throw err
callback(null, isMatch)
})
}
// 3. uncomment this when you want to switch back to deployed heroku
mongoose.connect("mongodb://zk377:[email protected]:47265/heroku_tzrlghms");
// mongoose.connect('mongodb://localhost/fp')