Auth Fetch and Server Health Update
This commit is contained in:
parent
dba68ed584
commit
2e5fed545f
|
|
@ -508,11 +508,7 @@ app.get("/forgot-password", (req, res) => {
|
|||
})
|
||||
|
||||
app.get("/heath", () => {
|
||||
if (Math.round(Math.random()) === 1) {
|
||||
return res.status(200).json({ "success": true, "message": "Server is Healthy" })
|
||||
} else {
|
||||
return res.status(403).json({ "success": false, "message": "Server is not Healthy" })
|
||||
}
|
||||
return res.status(200).json({ "success": true, "message": "Server is Healthy" })
|
||||
})
|
||||
|
||||
app.use((req, res) => {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import fetch from "node-fetch"; // or global fetch in Node 18+
|
||||
|
||||
let options = {
|
||||
let preOptions = {
|
||||
token: ''
|
||||
}
|
||||
|
||||
|
|
@ -23,8 +23,9 @@ const getAccessToken = async () => {
|
|||
|
||||
|
||||
console.log(response)
|
||||
options.token = (await response.json()).access_token;
|
||||
return options.token;
|
||||
const access_token = (await response.json()).access_token;
|
||||
preOptions.token = access_token;
|
||||
return access_token;
|
||||
} catch (error) {
|
||||
console.error('Error fetching access token:', error);
|
||||
throw error;
|
||||
|
|
@ -33,14 +34,32 @@ const getAccessToken = async () => {
|
|||
};
|
||||
|
||||
export const authFetch = async (url, options = {}) => {
|
||||
const token = await getAccessToken();
|
||||
let isvalid = false;
|
||||
|
||||
if (preOptions.token != "" && preOptions.token != null && preOptions.token != undefined) {
|
||||
isvalid = await fetch(`${process.env.SF_DOCTRACKER_TOKEN_VALIDATION_URL}services/apexrest/docva/health}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${preOptions.token}`,
|
||||
},
|
||||
});
|
||||
|
||||
const a = JSON.stringify(isvalid.body);
|
||||
console.log(a);
|
||||
}
|
||||
|
||||
if (isvalid.status !== 200) {
|
||||
await getAccessToken();
|
||||
}
|
||||
|
||||
|
||||
return fetch(url, {
|
||||
...options,
|
||||
headers: {
|
||||
...(options.headers || {}),
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
Authorization: `Bearer ${preOptions.token}`,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue