I have this authFetch function that fetches the token and updates it in mongo and token variable.
const authFetch = async () => {
let authCollection = client.db("WheelPros_data").collection("auth");
TokenFromDB = await authCollection.find({ _id: 1 }).toArray();
({ accessToken: token, time: lastFetchedAuth } = TokenFromDB[0] || [{ accessToken: null, time: 0 }])
// console.log(new Date().getTime() / 1000 - lastFetchedAuth)
let axiosConfig = {
headers: {
"Content-Type": "application/json",
accept: "application/json",
},
};
await axios({
method: "POST",
data: {
userName: process.env.WHEELPROS_USERNAME,
password: process.env.PASSWORD,
},
url: `${process.env.PROD_API_URL}/auth/v1/authorize`,
axiosConfig,
})
.then(async (res) => {
const { accessToken } = res.data;
authCollection.updateOne(
{ _id: 1 },
{
$set: {
accessToken: accessToken,
time: new Date().getTime() / 1000,
Date: new Date(),
},
}, {
upsert: true
}
);
console.log("newAccessToken : ", accessToken)
token = await accessToken
console.log("inside token var = ", token)
console.log("------------------Auth Updated------------------");
})
.catch((err) => {
console.log("AXIOS ERROR: ", err);
});
}
await authFetch()
// console.log("ourside token var = ", token)
setInterval(authFetch, 3590000)
Problem arises when i call another async function to fetch the submodels. After 1 hour the token expires and that is why we have setInterval function to update the token but it does not update the token and i get hit with the 403 error
Below is the modelFetch function
const fetchSubmodels = async () => {
const modelDataFromDB = await collection
.find({ modelData: { $exists: true } })
.toArray();
console.log(modelDataFromDB.length)
modelDataFromDB.forEach(async (modeldata) => {
// await fetchToken().catch(console.error)
let dataSearch = modeldata.modelData.split(';')
await axios
.get(
`${process.env.PROD_API_URL}/${dataSearch[0]}/makes/${dataSearch[1]}/models/${dataSearch[2]}/submodels`,
{
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
}
)
.then(async function ({ data }) {
await collection.updateOne(
{ modelData: modeldata.modelData },
{
$set: {
submodels: data
},
}
);
// handle success
console.log(`pushing the submodels ${data} to ${modeldata.modelData} `)
})
.catch(async function (error) {
// handle error
console.log(error);
})
})
}
await fetchSubmodels()
clearInterval(authFetch)
Any insights would be helpful!