menu

Questions & Answers

full calendar io events as json feed add bearer token

I am using the full calendar io events as a json feed to call a .net 6 web api.

client side react code

<FullCalendar
        initialView="dayGridMonth"
        headerToolbar={{
          left: "prev,next",
          center: "title",
          right: "dayGridMonth,timeGridWeek,timeGridDay"
        }}
        plugins={[dayGridPlugin, timeGridPlugin]}
        events={`${process.env.REACT_APP_API_URL}/activities/getEventsByDate/${id}`}
        eventClick={handleEventClick}
      />

the .net 6 web api controller.

  [AllowAnonymous]
        [HttpGet("GetEventsByDate/{routeName}")]
        public async Task<ActionResult> GetRoomEvents(string routeName)
        {
            string start = Request.Query["start"];
            string end = Request.Query["end"];
            return HandleResult(await Mediator.Send(new GetEventsByDate.Query { RouteName = routeName, Start = start, End = end }));
        }

however I would like to remove the AllowAnonymous and add my jwt bearer token. is their an option to do that using the full calendar io json feed?

Comments:
2023-01-19 23:25:05
You could use fullcalendar.io/docs/events-function where you can write any AJAX function you like to fetch the events, within a callback defined by you.
Answers(1) :

With Typescript, you could use the following to fetch the api with your token added:

 events: function (info, successCallback, failureCallback) {
            const response = fetch('api/calendar', {
                method: 'GET',
                headers: {
                    "Authorization": 'Bearer ' + token)
                }
            });
            const data = response;
         successCallback(data);
         failureCallback(data);
        },
    }