daisycrego / jbg-admin

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

events/ PUT requires `user` details for authentication #14

Closed daisycrego closed 3 years ago

daisycrego commented 3 years ago

Within the api-event.js (frontend wrappers around fetch calls to the express server endpoints), we have an update function which we will use to update the event status:

const update = async (params, credentials, event) => {
  console.log(`api-event: update()`);
  console.log(`params:`);
  console.log(params);
  console.log(`credentials:`);
  console.log(credentials);
  console.log(`event:`);
  console.log(event);
  try {
    // add userId as a parameter or somehow access userId from the route!
    let response = await fetch(`/api/events/${params.eventId}`, {
      method: "PUT",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        Authorization: "Bearer " + credentials.t,
      },
      body: JSON.stringify(event),
    });
    return await response.json();
  } catch (err) {
    console.log(err);
  }
};

router.param("eventId", eventCtrl.eventById);

- Every route should have `req.event` set because we actually set `req.event` in the call to `eventCtrl.eventById` that happens when the route is run:
```js
/**
 * Load event and append to req.
 */
const eventById = async (req, res, next, id) => {
  try {
    let event = await Event.findById(id);
    if (!event)
      return res.status("400").json({
        error: "event not found",
      });
    req.event = event;
    next();
  } catch (err) {
    return res.status("400").json({
      error: "Could not retrieve event",
    });
  }
};
daisycrego commented 3 years ago

Solution

daisycrego commented 3 years ago

req.profile was previously only being set if a route has userId as a parameter. Now, we're also defining req.profile for event routes as well, specifically the update route so far. This code isn't exactly in the right place right now... but it will work for all the events routes, so let's keep moving forward.