-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.js
143 lines (122 loc) · 4.34 KB
/
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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var metadataToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJTOHdpc2NCRVpyWHkzRDRNcUx2ekRPUGhlNUN2ZmtFcyIsInNjb3BlcyI6eyJ1c2VycyI6eyJhY3Rpb25zIjpbInJlYWQiXX0sInVzZXJzX2FwcF9tZXRhZGF0YSI6eyJhY3Rpb25zIjpbInJlYWQiLCJjcmVhdGUiLCJ1cGRhdGUiLCJkZWxldGUiXX19LCJpYXQiOjE0MjczNDAwNTAsImp0aSI6ImYwNWJlYjI4YzUyNWIwMTdiNzliNGMzZjExZTNhOWY4In0.1j6nDOZs7u3KJBvqnUZWjOnjnNa3kTYY81orfgINqPs';
var https = require('https');
var user = {};
var apiv2 = '/api/v2/';
function getRequestOption(path,method) {
var opt = {
hostname: 'login.auth0.com',
path: path,
method: method,
headers: {
'Accept':'application/json',
'Content-Type': 'application/json',
'Authorization':'Bearer ' + metadataToken
}
};
return opt;
}
function get(opt, callback) {
var req = https.request(opt, function(res) {
console.log('request result: ', res.statusCode);
res.setEncoding('utf8');
if(res.statusCode == 200) {
res.on('data', function(d) {
console.info('user:get:',d);
callback(null, JSON.parse(d));
});
} else {
res.on('data', function(d) {
console.error('failed on get: ', d);
callback(res.statusCode, null);
});
}
});
req.end();
req.on('error', function(e) {
console.log('request error: ', e);
callback(e, null);
});
}
function patch(path, opt, patchData, callback) {
var req = https.request(opt, function(res) {
console.log('addLock: status: ', res.statusCode);
res.setEncoding('utf8');
res.on('data', function(data) {
callback(null, data);
});
});
req.on('error', function(patchErr) {
console.log('Error: addLock: ', patchErr);
callback(e, null);
});
req.write(patchData);
req.end();
}
user.getProfile = function(userid, callback){
var path = apiv2 + 'users/' + userid;
var opt = getRequestOption(path, 'GET');
get(opt, callback);
};
user.getAppMetadata = function(userid, callback) {
var path = apiv2 + 'users/' + userid + '?fields=app_metadata';
var opt = getRequestOption(path, 'GET');
get(opt, callback);
};
user.addLock = function(userid, lockid, callback) {
user.getAppMetadata(userid, function(errMetadata, data) {
if(errMetadata) {
console.log('user.addLock: failed: ', errMetadata);
callback(errMetadata, data);
} else {
var locks = data.app_metadata.locks || [];
var index = locks.indexOf(lockid);
if(index === -1) {
locks.push(lockid);
data.app_metadata.locks = locks;
var path = apiv2 + 'users/' + userid;
var opt = getRequestOption(path, 'PATCH');
var appdata = JSON.stringify(data);
patch(path, opt, appdata, callback);
} else {
callback('lock already added', null);
}
}
});
};
user.removeLock = function(userid, lockid, callback) {
user.getAppMetadata(userid, function(errMetadata, data) {
if(errMetadata) {
console.log('user.removeLock: failed: ', errMetadata);
callback(errMetadata, data);
} else {
console.log('remove lock: current data:', data);
if(data.app_metadata.locks) {
var index = data.app_metadata.locks.indexOf(lockid);
if(index < 0) {
callback('Failure: lockid not found', null);
} else {
data.app_metadata.locks.splice(index, 1);
var path = apiv2 + 'users/' + userid;
var opt = getRequestOption(path, 'PATCH');
var appdata = JSON.stringify(data);
patch(path, opt, appdata, callback);
}
} else {
callback('Failure: lockid not found', null);
}
}
});
};
user.getLockList = function(userid, callback) {
user.getAppMetadata(userid, function(err, data) {
if(err) {
callback(err, null);
} else {
console.log('lockList: ', data);
console.log('locks', data.app_metadata.locks);
lockList = data.app_metadata.locks || [];
callback(null, lockList);
}
});
};
module.exports = user;