Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions app/routes/index.tsx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we will have to check for cookies on root.tsx as it's possible for a user to directly visit another route in which case this check will be bypassed.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect } from 'react';
import { LoaderFunction, json, redirect } from '@remix-run/node';
import { Outlet, useLoaderData } from '@remix-run/react';
import { Outlet, useLoaderData, useNavigate } from '@remix-run/react';
import { toast, ToastContainer } from 'react-toastify';
import axios from 'axios';
import dayjs from 'dayjs';
Expand All @@ -10,7 +10,7 @@ import { useStore } from '~/store/useStore';
import { parseEvents } from '~/utils/event.utils';
import { getEvents } from '~/constants/urls.constants';
import { getUrls } from '~/models/urls.server';
import { parseCookie } from '~/utils/cookie.utils';
import { clearCookies, parseCookie } from '~/utils/cookie.utils';

type LoaderData = {
apiHost?: string;
Expand Down Expand Up @@ -52,12 +52,11 @@ function CalendarPage() {
let calendarId = 0;
const startTime = dayjs().subtract(1, 'months').startOf('month').unix() * 1000;
const endTime = dayjs().add(1, 'months').endOf('month').unix() * 1000;

const navigate = useNavigate();
if (typeof window !== 'undefined') {
const cookie = parseCookie(document.cookie);
calendarId = Number(cookie.calendarId);
}

try {
const { data: eventsList } = await axios.get(
getEvents(apiHost, calendarId, startTime, endTime),
Expand All @@ -69,7 +68,11 @@ function CalendarPage() {
},
);
setEvents(parseEvents(eventsList.data));
} catch (error) {
} catch (error: any) {
if (error?.response?.status === 401) {
clearCookies();
navigate('/login');
}
toast.error('Unable to fetch the event Details', {
toastId: 'events_error',
});
Expand Down
10 changes: 10 additions & 0 deletions app/utils/cookie.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ export const parseCookie = (str: string) =>
acc[camelKey] = value;
return acc;
}, {} as Record<string, string>);

const clearCookie = (cookieName: string) => {
document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
};

export const clearCookies = () => {
clearCookie('rcal-session');
clearCookie('username');
clearCookie('calendar-id');
};