-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
39 lines (29 loc) · 841 Bytes
/
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
var mongodb= require("mongoose");
var urldb =process.env.MONGOLAB_URI2;
mongodb.connect(urldb);
var Schema = mongodb.Schema;
var bcrypt = require('bcrypt-nodejs');
var db = mongodb.connection;
db.on('error', function(err){
console.log('connection error', err);
});
db.once('open', function(){
console.log('Connection to DB successful');
});
var userSchema = Schema({
email: String,
name: String,
password: String,
country: String,
town: String
});
// hash the password
userSchema.methods.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.password);
};
var User = mongodb.model('users', userSchema);
module.exports = User;