daisycrego / jbg-admin

Admin app for Jill Biggs Group follow-up boss event management
1 stars 0 forks source link

Basic auth header for /events FUB API queries - make calls from the server code (not client code) #3

Closed daisycrego closed 3 years ago

daisycrego commented 3 years ago

Not able to access the environment variable I need to query to /events FUB API. There is an environment var set in .env and config which I can't access, BASIC_AUTHORIZATION. This is to be appended to Basic <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 like require('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 theBearerauth header as in some of the other API methods inclient/event/api-event.js`) and return the values queried from the database.

TODO: Create a /sync-events endpoint for the sync_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.

daisycrego commented 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);
  };

// 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}); } };