-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
98 lines (80 loc) · 2.89 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
////////////////
// dependices
var debug = require('debug')('niche-store');
var bcrypt = require('bcrypt');
var mongoose = require('mongoose');
var productSchema = new mongoose.Schema({
slug: { type: String, unique: true },
title: String,
description: String,
price: Number,
image: String,
catagory: [String],
rating: { type: Number, default: 0 }
});
var Product = mongoose.model('Product', productSchema);
// In Mongoose everything is derived from Schema.
// Here we create a schema called User with the following fields.
// Each field requires a type and optional additional properties, e.g. unique field? required field?
var userSchema = new mongoose.Schema({
email: { type: String, required: true, index: { unique: true }},
password: { type: String, required: true },
firstName: { type: String, required: true },
lastName: { type: String, required: true },
joined_on: { type: Date, default: Date.now() },
isAdmin: Boolean,
purchasedProducts: [{
game: { type: mongoose.Schema.Types.ObjectId, ref: 'Product' },
date: { type: Date, default: Date.now() }
}],
viewedProducts: [String],
lastLogin: Boolean
});
// Express middleware that hashes a password before it is saved to database
// The following function is invoked right when we called MongoDB save() method
// We can define middleware once and it will work everywhere that we use save() to save data to MongoDB
// The purpose of this middleware is to hash the password before saving to database, because
// we don't want to save password as plain text for security reasons
userSchema.pre('save', function (next) {
'use strict';
var user = this;
// only hash the password if it has been modified (or is new)
if (!user.isModified('password')) {
return next();
}
// generate a salt with 10 rounds
bcrypt.genSalt(10, function (err, salt) {
if (err) {
return next(err);
}
// hash the password along with our new salt
bcrypt.hash(user.password, salt, function(err, hash) {
if (err) {
return next(err);
}
// override the cleartext password with the hashed one
user.password = hash;
next();
});
});
});
// This middleware compares user's typed-in password during login with the password stored in database
userSchema.methods.comparePassword = function(candidatePassword, callback) {
'use strict';
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
if (err) {
return callback(err);
}
callback(null, isMatch);
});
};
// User model based on schema
var User = mongoose.model('User', userSchema);
debug("Connecting to database...");
//mongoose.connect('localhost');
mongoose.connect('mongodb://holleranj:[email protected]:45948/niche-store', function (err) {
debug("Unable to connect to database.", err);
});
debug("Connected to database.");
module.exports.Product = Product;
module.exports.User = User;