Closed daisycrego closed 3 years ago
In the Events.js
page (client-side), when the user hits "Sync Events", we call the client-side sync_events
function, passing it along credentials because the /events/sync
API route is protected:
const handleSyncEventsClick = () => {
const jwt = auth.isAuthenticated();
const credentials = { t: jwt.token };
const abortController = new AbortController();
const signal = abortController.signal;
sync_events(credentials, signal);
};
sync_events
function will actually call the /events/sync
API route:
const sync_events = async (credentials, signal) => {
try {
let response = await fetch("/api/events/sync", {
method: "GET",
signal: signal,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: "Bearer " + credentials.t,
},
});
return await response.json();
} catch (err) {
console.log(err);
}
};
events/sync
API route is responsible for retrieving the base-64 encoded FUB API key to use for the basic auth header, and will eventually also perform any db operations needed after retrieving the new events data:
// event.routes.js
router
.route("/api/events/sync")
.get(authCtrl.requireSignin, eventCtrl.syncEvents);
// events.controller.js
const syncEvents = async (req, res) => {
console.log(syncEvents API route:
);
const BASIC_AUTHORIZATION = process.env.BASIC_AUTHORIZATION;
console.log(BASIC_AUTHORIZATION:
);
console.log(BASIC_AUTHORIZATION);
//console.log(Events API: sync_events():
);
const url = "https://api.followupboss.com/v1/events?limit=10&offset=0";
const options = {
method: "GET",
headers: {
Accept: "application/json",
Authorization: Basic ${ BASIC_AUTHORIZATION || "MmM1OTRjYTI0M2QwZDliMDhlNDYyNzE0MjE4MmQ0YzMyMmZjYWU6Cg==" }
,
},
};
console.log(headers:
);
console.log(options.headers);
try {
const result = await fetch(url, options);
console.log(result:
);
console.log(result);
const json = await result.json();
console.log(json);
} catch (err) {
console.log(error: ${err}
);
}
};
Not able to access the environment variable I need to query to
/events
FUB API. There is an environment var set in.env
andconfig
which I can't access,BASIC_AUTHORIZATION
. This is to be appended toBasic <here>
for outgoing requests to the FUB API.When we have to load the config on the server side, we can confirm that things appear to work as planned. We can add some print statements into the definition of the config or the loading of
dotenv
and see that. The issue is happening in the client side code, and only when we look at the console logs on the browser do we see that the env vars are missing in that environment. We need to keep the environment variables on the server side, only do something likerequire('dotenv
).config()when in the server code. Otherwise, we should actually create an endpoint in the API which can take authenticated requests from the frontend (using the
Bearerauth header as in some of the other API methods in
client/event/api-event.js`) and return the values queried from the database.TODO: Create a
/sync-events
endpoint for thesync_events
method in client/event/api-event.js` to call. The method should be processing these incoming events, saving them to the database, and returning the events.