From cb7a6c8d05437d8ace8b564318aa58fd9602a57e Mon Sep 17 00:00:00 2001 From: Michael Fatemi Date: Thu, 15 Jul 2021 16:06:36 -0400 Subject: [PATCH] fix authentication issues --- src/components/App.tsx | 46 ++++++++++-------- .../Authentication/AuthenticationWrapper.tsx | 10 +++- .../Authentication/Authenticator.tsx | 6 --- src/components/Carpool/InvitationList.tsx | 48 +++++++++++-------- src/components/api.ts | 8 +++- 5 files changed, 68 insertions(+), 50 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index 567cf3e..a9f8780 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -2,7 +2,6 @@ import { CSSProperties, lazy, Suspense } from 'react'; import { BrowserRouter, Route, Switch } from 'react-router-dom'; import NotificationsProvider from '../state/Notifications/NotificationsProvider'; import { useMe } from './hooks'; -import UseImmutableTest from './UseImmutableTest'; import WheelShare from './WheelShare'; import WheelShareLoggedOut from './WheelShareLoggedOut'; @@ -25,30 +24,35 @@ export default function App() { const user = useMe(); return ( - -
- -
+
+
+ - - - - - - - + {user ? ( + + + + + + + + ) : ( + <> + + + + )} -
+
- +
); } diff --git a/src/components/Authentication/AuthenticationWrapper.tsx b/src/components/Authentication/AuthenticationWrapper.tsx index f367800..e4cbdd2 100644 --- a/src/components/Authentication/AuthenticationWrapper.tsx +++ b/src/components/Authentication/AuthenticationWrapper.tsx @@ -15,7 +15,15 @@ export default function AuthenticationWrapper({ const sessionToken = localStorage.getItem('session_token'); if (sessionToken) { - getMe().then(setUser).catch(none); + getMe() + .then((user) => { + if ('status' in user && user.status === 'error') { + setUser(null); + } else { + setUser(user); + } + }) + .catch(() => none()); } else { none(); } diff --git a/src/components/Authentication/Authenticator.tsx b/src/components/Authentication/Authenticator.tsx index ba95556..677b9c4 100644 --- a/src/components/Authentication/Authenticator.tsx +++ b/src/components/Authentication/Authenticator.tsx @@ -75,9 +75,6 @@ export default function Authenticator() { <>

Sign In Error

We couldn't use your Ion account to log in. -
-
- Home ); break; @@ -94,9 +91,6 @@ export default function Authenticator() { children = ( <>

Sign In Error

-
-
- Home ); } diff --git a/src/components/Carpool/InvitationList.tsx b/src/components/Carpool/InvitationList.tsx index 85b1ac9..103d278 100644 --- a/src/components/Carpool/InvitationList.tsx +++ b/src/components/Carpool/InvitationList.tsx @@ -42,14 +42,12 @@ function InvitationRow({ export default function InvitationList() { const { carpool } = useContext(CarpoolContext); - const eventId = carpool.event.id; - const [availableSignups, setAvailableSignups] = useImmutable(null); useEffect(() => { - getPotentialInvitees(eventId).then(setAvailableSignups); - }, [eventId, setAvailableSignups]); + getPotentialInvitees(carpool.id).then(setAvailableSignups); + }, [carpool.id, setAvailableSignups]); const invitedUserIDs = useMemo( () => @@ -63,9 +61,11 @@ export default function InvitationList() { const availableSignupsAlreadyInvited = useMemo( () => - availableSignups?.filter((signup) => - invitedUserIDs.has(signup.user.id) - ) ?? null, + availableSignups + ? availableSignups.filter((signup) => + invitedUserIDs.has(signup.user.id) + ) + : null, [availableSignups, invitedUserIDs] ); @@ -90,20 +90,26 @@ export default function InvitationList() { >

Invite Somebody

{availableSignups === null && 'Loading'} - {availableSignupsAlreadyInvited?.map((signup) => ( - - ))} - {availableSignupsNotInvited?.map((signup) => ( - - ))} + {availableSignupsAlreadyInvited?.map( + (signup) => + !carpool.members.some(({ id }) => id === signup.user.id) && ( + + ) + )} + {availableSignupsNotInvited?.map( + (signup) => + !carpool.members.some(({ id }) => id === signup.user.id) && ( + + ) + )}
); } diff --git a/src/components/api.ts b/src/components/api.ts index 34c7b68..93badd7 100644 --- a/src/components/api.ts +++ b/src/components/api.ts @@ -44,7 +44,13 @@ async function get(path: string) { Authorization: 'Bearer ' + localStorage.getItem('session_token'), }, }); - return await res.json(); + const result = await res.json(); + return result; + // if (res.ok) { + // return result; + // } else { + // throw new Error(result.message); + // } } export async function getEventSignups(