-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupport-tokens.js
75 lines (73 loc) · 2.07 KB
/
support-tokens.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 my_type = 'tokens'
module.exports = {
//**********************************************************
//This function is used to register new users
//example of use:
register_token: function(es_server, my_index, user_id, currenttime, expirationtime) {
return new Promise( (resolve,reject) => {
var elasticsearch = require('elasticsearch');
var clientb = new elasticsearch.Client({
host: es_server,
log: 'error'
});
var count_tokens = this.query_tokens(es_server, my_index,user_id, currenttime, expirationtime);
count_tokens.then((resultCount) => {
if(resultCount!=0){
resolve ("Could not register an existing token.");
}else{
clientb.index({
index: my_index,
type: my_type,
body: {
"user_id":user_id,
"currenttime": currenttime,
"expirationtime": expirationtime
}
}, function(error, response) {
if (error !== 'undefined') {
reject (error);
} else {
reject ("Could not register the user/pw.");
}
});//end query client.index
resolve ("succeed");
}
},(resultReject)=> {
reject (resultReject);
});//end count_users
});//end promise
}, //end register
//****************************************************
//This function is used to confirm that an user exists or not in the DataBase.
query_tokens: function(es_server, my_index, user_id, currenttime, expirationtime){
return new Promise( (resolve,reject) => {
var size =0;
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: es_server,
log: 'error'
});
client.count({
index: my_index,
type: my_type,
body: {
query:{bool:{must:[
{match:{"user_id":user_id }},
{match:{"currenttime": currenttime }},
{match:{"expirationtime": expirationtime }}
]}}
}
}, function(error, response) {
if (error) {
reject (error);
}
if (response.count !== undefined) {
size = response.count;
}else{
size=0;
}
resolve (size);
});
});
}//end query_user
}//end module.exports