-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
38 lines (30 loc) · 944 Bytes
/
auth.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
const axios = require('axios');
const qs = require('qs');
const coder = require('./coder');
module.exports = class Auth {
static token = '';
static async getToken() {
if (!Auth.token.length) {
try {
Auth.token = await Auth.fetchToken();
} catch (error) {
console.log(error.response);
}
}
return Auth.token;
}
static setToken(token) {
Auth.token = token;
}
static async fetchToken() {
let payload = {
audience: process.env.API_IDENTIFIER,
grant_type: process.env.API_GRANT_TYPE,
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
};
let url = `https://${process.env.API_DOMAIN}oauth/token`;
const response = await axios.post(url, payload);
return Promise.resolve(response.data.access_token);
}
};