-
Notifications
You must be signed in to change notification settings - Fork 90
/
functions.js
72 lines (66 loc) · 2.4 KB
/
functions.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
var bcrypt = require('bcryptjs'),
Q = require('q'),
config = require('./config.js'), //config file contains all tokens and other private info
db = require('orchestrate')(config.db); //config.db holds Orchestrate token
//used in local-signup strategy
exports.localReg = function (username, password) {
var deferred = Q.defer();
var hash = bcrypt.hashSync(password, 8);
var user = {
"username": username,
"password": hash,
"avatar": "http://placepuppy.it/images/homepage/Beagle_puppy_6_weeks.JPG"
}
//check if username is already assigned in our database
db.get('local-users', username)
.then(function (result){ //case in which user already exists in db
console.log('username already exists');
deferred.resolve(false); //username already exists
})
.fail(function (result) {//case in which user does not already exist in db
console.log(result.body);
if (result.body.message == 'The requested items could not be found.'){
console.log('Username is free for use');
db.put('local-users', username, user)
.then(function () {
console.log("USER: " + user);
deferred.resolve(user);
})
.fail(function (err) {
console.log("PUT FAIL:" + err.body);
deferred.reject(new Error(err.body));
});
} else {
deferred.reject(new Error(result.body));
}
});
return deferred.promise;
};
//check if user exists
//if user exists check if passwords match (use bcrypt.compareSync(password, hash); // true where 'hash' is password in DB)
//if password matches take into website
//if user doesn't exist or password doesn't match tell them it failed
exports.localAuth = function (username, password) {
var deferred = Q.defer();
db.get('local-users', username)
.then(function (result){
console.log("FOUND USER");
var hash = result.body.password;
console.log(hash);
console.log(bcrypt.compareSync(password, hash));
if (bcrypt.compareSync(password, hash)) {
deferred.resolve(result.body);
} else {
console.log("PASSWORDS NOT MATCH");
deferred.resolve(false);
}
}).fail(function (err){
if (err.body.message == 'The requested items could not be found.'){
console.log("COULD NOT FIND USER IN DB FOR SIGNIN");
deferred.resolve(false);
} else {
deferred.reject(new Error(err));
}
});
return deferred.promise;
}