-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (39 loc) · 1.17 KB
/
index.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
var request = require("request");
require('dotenv-safe').load();
var cache = require('memory-cache');
var options = { method: 'POST',
url: 'https://kachhalimbu.auth0.com/oauth/token',
headers: { 'content-type': 'application/json' },
body:
{ grant_type: 'client_credentials',
client_id: process.env.CLIENT_ID ,
client_secret: process.env.CLIENT_SECRET,
audience: 'https://kachhalimbu.auth0.com/api/v2/' },
json: true };
function getManagementToken(cb) {
var cached = cache.get(process.env.CLIENT_ID);
if (cached) {
console.log("returning cached token");
cb(null, cached);
} else {
console.log("getting a new one");
request(options, function (error, response, body) {
if (error) {
console.log(error);
throw new Error(error);
}
console.log(body);
cache.put(process.env.CLIENT_ID, body, body.expires_in * 1000 || 10000);
cb(null, body);
});
}
}
// TODO: Clean up this and add a proper test.
getManagementToken(function() {
console.log('Got first token');
});
var counter = 1;
setInterval(function() {
getManagementToken(function() {
console.log('Got token ' + counter++);
})}, 9000);