-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
45 lines (40 loc) · 1.29 KB
/
auth.js
File metadata and controls
45 lines (40 loc) · 1.29 KB
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
const req = require('tiny_request');
const config = require('./config');
class Auth {
static getHeaders($) {
return Auth.headers[$.provider.name];
}
static run(onAllReqsSucceedFirstTime) {
Auth.providers = config.contentProviders;
Auth.providersCount = Object.keys(Auth.providers).length;
Auth.readyProvidersCount = 0;
Auth.headers = {};
Object.keys(Auth.providers).forEach(providerName => {
const providerHeaders = Auth.headers[providerName] = {};
Object.assign(providerHeaders, config.defaultXhrHeaders);
});
Auth.sendSessionRequests(onAllReqsSucceedFirstTime);
setInterval(Auth.sendSessionRequests.bind(Auth), 15 * 1000 * 60);
}
static sendSessionRequests(onReqsSucceed = () => {}) {
Object.keys(Auth.providers).forEach(Auth.sendSessionRequest.bind(Auth, onReqsSucceed));
}
static sendSessionRequest(onReqsSucceed, providerName) {
const provider = Auth.providers[providerName];
const headers = Auth.headers[providerName];
req.get({
url: provider.apiUrl + provider.anonymousAuthPath,
query: provider.anonymousAuthQueryParams,
headers,
json: true
}, body => {
if (body) {
headers[config.sessionHeaderKey] = body.sessionId;
if (++Auth.readyProvidersCount === Auth.providersCount) {
onReqsSucceed();
}
}
});
}
}
module.exports = Auth;