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", () => {
|
app.get("/heath", () => {
|
||||||
if (Math.round(Math.random()) === 1) {
|
|
||||||
return res.status(200).json({ "success": true, "message": "Server is Healthy" })
|
return res.status(200).json({ "success": true, "message": "Server is Healthy" })
|
||||||
} else {
|
|
||||||
return res.status(403).json({ "success": false, "message": "Server is not Healthy" })
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
app.use((req, res) => {
|
app.use((req, res) => {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import fetch from "node-fetch"; // or global fetch in Node 18+
|
import fetch from "node-fetch"; // or global fetch in Node 18+
|
||||||
|
|
||||||
let options = {
|
let preOptions = {
|
||||||
token: ''
|
token: ''
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -23,8 +23,9 @@ const getAccessToken = async () => {
|
||||||
|
|
||||||
|
|
||||||
console.log(response)
|
console.log(response)
|
||||||
options.token = (await response.json()).access_token;
|
const access_token = (await response.json()).access_token;
|
||||||
return options.token;
|
preOptions.token = access_token;
|
||||||
|
return access_token;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching access token:', error);
|
console.error('Error fetching access token:', error);
|
||||||
throw error;
|
throw error;
|
||||||
|
|
@ -33,14 +34,32 @@ const getAccessToken = async () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const authFetch = async (url, options = {}) => {
|
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, {
|
return fetch(url, {
|
||||||
...options,
|
...options,
|
||||||
headers: {
|
headers: {
|
||||||
...(options.headers || {}),
|
...(options.headers || {}),
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${preOptions.token}`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue