From 4f01d0bbd031042bc3868cfd133dbbefcfb0cc61 Mon Sep 17 00:00:00 2001 From: aidanprior Date: Wed, 30 Oct 2024 12:31:20 -0400 Subject: [PATCH 1/4] fix: fix some edge cases for routing on the frontend, organized user session managment better --- client/src/App.tsx | 60 +++++++++++++++++++++++++++----- client/src/components/Navbar.tsx | 2 +- client/src/pages/Login.tsx | 13 +------ client/src/pages/Profile.tsx | 31 ++++------------- client/src/pages/SignUp.tsx | 2 +- client/src/types.ts | 2 +- compose.yml | 1 - 7 files changed, 63 insertions(+), 48 deletions(-) diff --git a/client/src/App.tsx b/client/src/App.tsx index 2484db0..47501b0 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -1,4 +1,4 @@ -import React, { ReactNode, useState } from 'react'; +import React, { ReactNode, useCallback, useEffect, useState } from 'react'; import { BrowserRouter as Router, Route, Routes } from 'react-router-dom'; import Navbar from './components/Navbar'; @@ -7,12 +7,52 @@ import Home from './pages/Home'; import EventsDashboard from './pages/EventsDashboard'; import Login from './pages/Login'; import SignUp from './pages/SignUp'; -import { UserDetails } from './types'; +import { AWSCredentials, UserDetails } from './types'; const App: React.FC = () => { const [isDarkMode, setIsDarkMode] = useState(false); // Dark mode state const [user, setUser] = useState(null); + const updateCredentials = useCallback( + function (credentials: AWSCredentials): void { + fetch('/credentials', { + method: 'POST', + body: JSON.stringify({ + ...credentials, + username: user?.username ?? 'No Active User', + }), + headers: { + 'Content-Type': 'application/json', + }, + }) + .then((response) => { + if (!response.ok) + throw Error('Server Error while updating aws credentials'); + return response.json(); + }) + .then((data: UserDetails) => { + setUser(data); + window.localStorage.setItem('user', JSON.stringify(data)); + }) + .catch((error: Error) => { + console.error(error); + }); + }, + // we don't want to update on user update, because it would create an infinte loop, only on app reload + // eslint-disable-next-line react-hooks/exhaustive-deps + [] + ); + + // check for a user session and update the user if found + useEffect(() => { + if (window.localStorage.getItem('user')) { + const locallyStoredUser: UserDetails = JSON.parse( + window.localStorage.getItem('user')! + ) as UserDetails; + updateCredentials(locallyStoredUser); + } + }, [updateCredentials]); + const toggleDarkMode = () => { setIsDarkMode((prev) => !prev); document.body.classList.toggle('dark-mode', !isDarkMode); // Toggle class based on state @@ -47,26 +87,30 @@ const App: React.FC = () => { setUser={setUser} /> + } /> } /> } /> ))} + path="/home" + element={checkLogin(checkAWSCreds())} /> + )} /> ) + element={checkLogin( + checkAWSCreds() )} /> - {/* } */} ); diff --git a/client/src/components/Navbar.tsx b/client/src/components/Navbar.tsx index a29eb59..3903b0d 100644 --- a/client/src/components/Navbar.tsx +++ b/client/src/components/Navbar.tsx @@ -43,7 +43,7 @@ const Navbar: React.FC = ({ return (