-
Notifications
You must be signed in to change notification settings - Fork 0
/
manip_usr.js
75 lines (70 loc) · 1.85 KB
/
manip_usr.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
var mongoose = require('mongoose');
var usrSchema = mongoose.Schema({
email: {type: String, required: true, unique: true},
passwd: String
});
var Users = mongoose.model('urs', usrSchema);
var verify_delete_usr = function(email, passwd, callback)
{
var promise = Users.find({email: email});
promise.then(function(data) {
if (data.length == 0)
throw ("no user")
return (data)
})
.then(function(data){
var str;
var keyValue = {};
str = data.toString();
str.split(",\n ").forEach((element) => {
keyValue[element.split(': ')[0]] = element.split(': ')[1];
})
if (keyValue["passwd"] == null)
throw ("no password")
if (keyValue["passwd"].localeCompare( "\'" + passwd + "\'") == 0) {
promise.verified = true;
}
})
.then(function(){
if (promise.verified === true){
var res = ""
Users.remove({email: email}, function(err, result) {
if (err)
console.log(err);
}, 1)
callback("delete_succes");
}
})
.catch(function(err){
callback("delete_failure");
})
}
module.exports = {
create_usr: function(email, password)
{
var newUsr = new Users;
newUsr['email'] = email;
newUsr['passwd'] = password;
newUsr.save();
},
modify_usr: function(email, password)
{
Users.update({email : email},
{passwd: password},
{upsert: true},
function(err, data)
{
if (err)
console.log(err);
else
console.log(data);
});
},
delete_usr: function(email, passwd, callback)
{
verify_delete_usr(email, passwd, function(mess)
{
callback(mess);
})
}
}