43 lines
969 B
JavaScript
43 lines
969 B
JavaScript
import axios from "axios";
|
|
import { ACCESS_TOKEN_ENDPOINT, TENANT_ID } from "./env";
|
|
import { consoleLogger } from "./logger";
|
|
|
|
const cache = {};
|
|
|
|
const tokenBody =
|
|
"grant_type=" +
|
|
process.env.GRANT_TYPE +
|
|
"&client_id=" +
|
|
process.env.CLIENT_ID +
|
|
"&client_secret=" +
|
|
process.env.CLIENT_SECRET +
|
|
"&scope=" +
|
|
"https://" +
|
|
process.env.RELAYURI +
|
|
"/.default";
|
|
|
|
const tokenConfig = {
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
"Cache-Control": "no-cache"
|
|
}
|
|
};
|
|
|
|
export const getToken = () => {
|
|
return axios
|
|
.post(
|
|
`${ACCESS_TOKEN_ENDPOINT}${TENANT_ID}/oauth2/v2.0/token`,
|
|
tokenBody,
|
|
tokenConfig
|
|
)
|
|
.then((res) => res.data)
|
|
.then((data) => {
|
|
cache.tokenResponse = data;
|
|
return data;
|
|
})
|
|
.catch((error) => {
|
|
consoleLogger(error);
|
|
return error;
|
|
});
|
|
};
|