-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
63 lines (50 loc) · 1.33 KB
/
index.js
File metadata and controls
63 lines (50 loc) · 1.33 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"use strict";
require("dotenv").config();
const axios = require("axios");
const querystring = require("querystring");
class GigyaApi {
constructor(loginToken) {
this.setLoginToken(loginToken);
}
setLoginToken(loginToken) {
this.loginToken = loginToken;
}
async call(endpoint, params = {}) {
if (endpoint !== "accounts.login") {
params = {
login_token: this.loginToken,
authMode: "cookie",
...params
};
}
const response = await axios.post(
`${process.env.GIGYA_API_ROOT}/${endpoint}`,
querystring.stringify({
ApiKey: process.env.GIGYA_API_KEY,
...params
})
);
if (response.data.errorDetails) {
throw Error(response.data.errorDetails);
}
return response.data;
}
async fetchLoginToken(loginID, password) {
const data = await this.call("accounts.login", { loginID, password });
const { cookieValue } = data.sessionInfo;
return cookieValue;
}
async fetchPersonId() {
const data = await this.call("accounts.getAccountInfo");
const { personId } = data.data;
return personId;
}
async fetchJWTToken() {
const data = await this.call("accounts.getJWT", {
fields: "data.personId,data.gigyaDataCenter"
});
const { id_token } = data;
return id_token;
}
}
module.exports = GigyaApi;