From 9dceab94a832def33ed8611564a1eb0f71ff148c Mon Sep 17 00:00:00 2001 From: Marcia Date: Mon, 9 Dec 2024 22:06:46 -0600 Subject: [PATCH 01/22] Trying to fix retained token bug --- frontend/src/App.jsx | 49 ++++++++++++++++--- frontend/src/components/DashCardSet.jsx | 5 +- frontend/src/components/DashTable.jsx | 6 ++- frontend/src/components/EditModal.jsx | 2 + frontend/src/components/Navigation.jsx | 3 ++ frontend/src/components/NewKit.jsx | 3 ++ frontend/src/components/NewKitItem.jsx | 3 ++ frontend/src/components/NewUser.jsx | 3 ++ frontend/src/components/auth/CurrentUser.jsx | 11 +++-- frontend/src/components/auth/Login.jsx | 11 +++-- frontend/src/components/auth/Logout.jsx | 8 ++- .../src/components/pages/AdminDashboard.jsx | 33 +++++++++++-- frontend/src/components/pages/Contact.jsx | 5 +- frontend/src/components/pages/Kits.jsx | 17 +++---- frontend/src/components/pages/RequestKit.jsx | 5 +- frontend/src/main.jsx | 3 ++ 16 files changed, 132 insertions(+), 35 deletions(-) diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 5bf53a65..f6be4b72 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -1,5 +1,5 @@ import './App.css'; -import {BrowserRouter as Router, Routes, Route, Navigate} from "react-router-dom"; +import {BrowserRouter as Router, Routes, Route, Navigate, useNavigate } from "react-router-dom"; import PageWrapper from './components/PageWrapper'; import Home from './components/pages/Home'; import About from './components/pages/About'; @@ -30,6 +30,7 @@ function App() { const [loggedIn, setLoggedIn] = useState(); const [user, setUser] = useState(null); const [tokenExpiration, setTokenExpiration] = useState(null); + const navigate = useNavigate(); // Method handles login state and token, checking for existence or expiration useEffect(() => { @@ -49,7 +50,7 @@ function App() { // Notify 5 minutes before expiration if (timeUntilExpiration < 300000) { - alert("Your session is about to expire. Please save your work."); + alert("Your session will expire soon."); } // Set token expiration time in state @@ -62,17 +63,23 @@ function App() { setLoggedIn(false); setUser(null); alert("Your session has expired. Please log in again."); + navigate('/login') + } } catch (error) { console.error('Token decoding failed:', error); localStorage.removeItem('jwt'); setLoggedIn(false); setUser(null); + navigate('/'); + } } else { // No token, set to logged out state setLoggedIn(false); setUser(null); + navigate('/') + alert("A network error occurred. You have been logged out.") } }, []); @@ -85,31 +92,58 @@ function App() { setLoggedIn(false); setUser(null); alert("Your session has expired. Please log in again."); + navigate('/login'); }, tokenExpiration); return () => clearTimeout(timer); } }, [tokenExpiration]); + const checkToken = async () => { + try { + const response = await fetch(userUrl, { + headers: { + Authorization: `Bearer ${localStorage.getItem('jwt')}`, + }, + }); + + if (response.ok) { + const data = await response.json(); + setUser(data); + } else { + console.log("No user logged in."); + setUser(null); + setLoggedIn(false); + localStorage.removeItem('jwt'); + const error = await response.json(); + console.log(error); + } + } catch (error) { + console.error("Error fetching current user"); + setUser(null); + setLoggedIn(false); + localStorage.removeItem('jwt'); + } + }; + return ( // Sets routes for app navigation and passes props to the necessary components <>
- } /> } /> - } /> + } /> } /> - } /> + } /> } /> }/> } /> - } /> + } /> }/> - : } /> + : } /> } > } /> } /> @@ -118,7 +152,6 @@ function App() { -
); diff --git a/frontend/src/components/DashCardSet.jsx b/frontend/src/components/DashCardSet.jsx index 12a60d34..7dea7e1a 100644 --- a/frontend/src/components/DashCardSet.jsx +++ b/frontend/src/components/DashCardSet.jsx @@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react'; import DashboardCard from './DashboardCard'; import { API_URL } from '../constants'; -const DashCardSet = () => { +const DashCardSet = ({ setUser, setLoggedIn }) => { const [userCount, setUserCount] = useState(0); const [kitRequestCount, setKitRequestCount] = useState(0); const [totalDonations, setTotalDonations] = useState(0); @@ -29,6 +29,9 @@ const DashCardSet = () => { } } catch (error) { console.error('Error fetching dashboard data:', error); + setLoggedIn(false); + setUser(null); + localStorage.removeItem('jwt'); } }; diff --git a/frontend/src/components/DashTable.jsx b/frontend/src/components/DashTable.jsx index 74023b28..dfb5475b 100644 --- a/frontend/src/components/DashTable.jsx +++ b/frontend/src/components/DashTable.jsx @@ -1,7 +1,7 @@ import React, { useEffect, useState } from 'react'; // Passed in api endpoint, headers for tables, and the event handler for showing the Edit Modal from Admin Dashboard component -const DashTable = ({ apiEndpoint, headers, handleShow }) => { +const DashTable = ({ apiEndpoint, headers, handleShow, setLoggedIn, setUser }) => { const [data, setData] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); @@ -28,6 +28,10 @@ const DashTable = ({ apiEndpoint, headers, handleShow }) => { } catch (err) { setError(err.message); console.error("Error fetching data:", err); + setLoggedIn(false); + setUser(null); + localStorage.removeItem('jwt'); + alert("A network error occurred.") } finally { setLoading(false); } diff --git a/frontend/src/components/EditModal.jsx b/frontend/src/components/EditModal.jsx index 4c6f36d6..f056312b 100644 --- a/frontend/src/components/EditModal.jsx +++ b/frontend/src/components/EditModal.jsx @@ -96,6 +96,8 @@ if (selectedImage) { console.log(`${recordType} updated successfully!`); alert(`${recordType} updated successfully!`); handleClose(); // Close the modal after success + } else { + alert("An error occurred with the update.") } }; diff --git a/frontend/src/components/Navigation.jsx b/frontend/src/components/Navigation.jsx index ebc64a7e..54d0dbc7 100644 --- a/frontend/src/components/Navigation.jsx +++ b/frontend/src/components/Navigation.jsx @@ -13,6 +13,9 @@ function Navigation({ loggedIn, setLoggedIn, setUser, user }) { if (!user) { // If user not logged in, navigate to login page alert("You must be logged in to make a donation. Please log in or register if you haven't already."); + setLoggedIn(false); + setUserData(null); + localStorage.removeItem('jwt'); navigate("/login") } else { // If user is logged in, navigate to the donation page diff --git a/frontend/src/components/NewKit.jsx b/frontend/src/components/NewKit.jsx index a8147166..3dc34fe1 100644 --- a/frontend/src/components/NewKit.jsx +++ b/frontend/src/components/NewKit.jsx @@ -68,6 +68,9 @@ const NewKit = () => { } catch (error) { setMessages("An error occurred: " + error.message); + setLoggedIn(false); + setUserData(null); + localStorage.removeItem('jwt'); console.log(error.message); } }; diff --git a/frontend/src/components/NewKitItem.jsx b/frontend/src/components/NewKitItem.jsx index 5f10f24b..864b7a67 100644 --- a/frontend/src/components/NewKitItem.jsx +++ b/frontend/src/components/NewKitItem.jsx @@ -60,6 +60,9 @@ const NewKitItem = () => { setMessages("An error occurred: " + error.message); console.log(error.message); + setLoggedIn(false); + setUserData(null); + localStorage.removeItem('jwt'); } }; diff --git a/frontend/src/components/NewUser.jsx b/frontend/src/components/NewUser.jsx index af035d08..37f112d2 100644 --- a/frontend/src/components/NewUser.jsx +++ b/frontend/src/components/NewUser.jsx @@ -65,6 +65,9 @@ const NewUser = () => { setRegistrationMessages("An error occurred: " + error.message); console.log(error.message); + setLoggedIn(false); + setUserData(null); + localStorage.removeItem('jwt'); } }; diff --git a/frontend/src/components/auth/CurrentUser.jsx b/frontend/src/components/auth/CurrentUser.jsx index 2fa29326..2cfc7e45 100644 --- a/frontend/src/components/auth/CurrentUser.jsx +++ b/frontend/src/components/auth/CurrentUser.jsx @@ -21,16 +21,21 @@ const CurrentUser = ({ setLoggedIn, setUser, user }) => { } else { console.log("No user logged in.") setUser(null); - const error = await response.json() + setLoggedIn(false); + localStorage.removeItem('jwt'); + const error = await response.json(); console.log(error) } } catch (error) { console.error("Error fetching current user"); + setUser(null); + setLoggedIn(false); + localStorage.removeItem('jwt'); } }}; fetchUser(); - }, [setLoggedIn, user, setUser, userUrl]); + }, [setLoggedIn, user, setUser ]); // Stretch Goal: Add admin dashboard if (!user) return null; @@ -40,7 +45,7 @@ const CurrentUser = ({ setLoggedIn, setUser, user }) => {

Welcome, {user.first_name}!

- {user.role === 'admin' && + {user && user.role === 'admin' && } ); diff --git a/frontend/src/components/auth/Login.jsx b/frontend/src/components/auth/Login.jsx index 4d63c8f4..e2f11b80 100644 --- a/frontend/src/components/auth/Login.jsx +++ b/frontend/src/components/auth/Login.jsx @@ -52,7 +52,6 @@ export default function Login({setLoggedIn}) { if (jwt) { // Store the JWT in local storage localStorage.setItem('jwt', jwt.split(' ')[1]); - console.log(localStorage.getItem('jwt')); } console.log("Login successful!"); @@ -69,7 +68,10 @@ export default function Login({setLoggedIn}) { } else { // Handle registration error const errorData = await response.json(); - setLoginMessages(errorData.status.message || "Login failed"); + setLoginMessages('Login failed. Please check your credentials and try again.'); + setLoggedIn(false); + setUserData(null); + localStorage.removeItem('jwt'); // Access the status and message in the JSON response @@ -79,7 +81,10 @@ export default function Login({setLoggedIn}) { } catch (error) { // Handle other errors console.log("An error occurred:", error) - setLoginMessages(error) + setLoginMessages("Unable to connect to the server. Please try again later."); + setUserData(null); + setLoggedIn(false); + localStorage.removeItem("jwt"); } }; diff --git a/frontend/src/components/auth/Logout.jsx b/frontend/src/components/auth/Logout.jsx index 51c1e5aa..ebdd8f43 100644 --- a/frontend/src/components/auth/Logout.jsx +++ b/frontend/src/components/auth/Logout.jsx @@ -55,15 +55,19 @@ export default function Logout({ setLoggedIn, setUser }) { const message = response.json(); setErrorMessage(message.error.message); console.error(errorMessage); - alert("Your session has expired, please sign in again."); + localStorage.removeItem("jwt"); setLoggedIn(false); setUser(null); - navigate("/login") + navigate("/login"); + alert("Your session has expired, please sign in again."); } } catch (error) { console.error("An error occurred:", error.message); + localStorage.removeItem("jwt"); + setUser(null); + setLoggedIn(false); alert(error.message) } diff --git a/frontend/src/components/pages/AdminDashboard.jsx b/frontend/src/components/pages/AdminDashboard.jsx index 0e8bf23b..ccb21edb 100644 --- a/frontend/src/components/pages/AdminDashboard.jsx +++ b/frontend/src/components/pages/AdminDashboard.jsx @@ -8,7 +8,7 @@ import DataEndpoint from "../DataEndpoint"; import DashTable from "../DashTable"; import EditModal from "../EditModal"; -const AdminDashboard = ({ user }) => { +const AdminDashboard = ({ user, setLoggedIn, setUser }) => { // Necessary api endpoints const userUrl = `${API_URL}/users`; const kitsUrl = `${API_URL}/kits`; @@ -141,6 +141,8 @@ const AdminDashboard = ({ user }) => { headers={headers.userUrl} apiEndpoint={userUrl} handleShow={(item) => handleShow(item, "user")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> ); const KitsTable = () => ( @@ -148,6 +150,8 @@ const AdminDashboard = ({ user }) => { headers={headers.kitsUrl} apiEndpoint={kitsUrl} handleShow={(item) => handleShow(item, "kit")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> ); const KitItemsTable = () => ( @@ -155,6 +159,8 @@ const AdminDashboard = ({ user }) => { header={headers.kitItemsUrl} apiEndpoint={kitItemsUrl} handleShow={(item) => handleShow(item, "kitItem")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> ); const KitRequestsTable = () => ( @@ -162,6 +168,8 @@ const AdminDashboard = ({ user }) => { header={headers.kitRequestsUrl} apiEndpoint={kitRequestsUrl} handleShow={(item) => handleShow(item, "kitRequest")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> ); const DonationsTable = () => ( @@ -169,6 +177,8 @@ const AdminDashboard = ({ user }) => { header={headers.donationUrl} apiEndpoint={donationUrl} handleShow={(item) => handleShow(item, "donation")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> ); const ContactsTable = () => ( @@ -176,6 +186,8 @@ const AdminDashboard = ({ user }) => { header={headers.contactsUrl} apiEndpoint={contactsUrl} handleShow={(item) => handleShow(item, "contact")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> ); @@ -207,7 +219,7 @@ const AdminDashboard = ({ user }) => { }, [selectedEndpoint]); - if (user.role !== "admin") { + if (!user || user.role !== "admin") { return (
@@ -216,7 +228,6 @@ const AdminDashboard = ({ user }) => {
); } - console.log("selectedEndpoint:", selectedEndpoint); return ( // Displays cards, graph data, and Data tables @@ -231,7 +242,7 @@ const AdminDashboard = ({ user }) => { > - Admin Dashboard + Admin Dashboard @@ -278,7 +289,7 @@ const AdminDashboard = ({ user }) => {
- +
@@ -310,6 +321,8 @@ const AdminDashboard = ({ user }) => { headers={headers.userUrl} apiEndpoint={userUrl} handleShow={(item) => handleShow(item, "user")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === kitsUrl && ( @@ -317,6 +330,8 @@ const AdminDashboard = ({ user }) => { headers={headers.kitsUrl} apiEndpoint={kitsUrl} handleShow={(item) => handleShow(item, "kit")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === kitItemsUrl && ( @@ -324,6 +339,8 @@ const AdminDashboard = ({ user }) => { headers={headers.kitItemsUrl} apiEndpoint={kitItemsUrl} handleShow={(item) => handleShow(item, "kitItem")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === kitRequestsUrl && ( @@ -333,6 +350,8 @@ const AdminDashboard = ({ user }) => { handleShow={(item) => handleShow(item, "kitRequest") } + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === donationUrl && ( @@ -340,6 +359,8 @@ const AdminDashboard = ({ user }) => { headers={headers.donationUrl} apiEndpoint={donationUrl} handleShow={(item) => handleShow(item, "donation")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === contactsUrl && ( @@ -347,6 +368,8 @@ const AdminDashboard = ({ user }) => { headers={headers.contactsUrl} apiEndpoint={contactsUrl} handleShow={(item) => handleShow(item, "contact")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} diff --git a/frontend/src/components/pages/Contact.jsx b/frontend/src/components/pages/Contact.jsx index 41f1f012..d20b4228 100644 --- a/frontend/src/components/pages/Contact.jsx +++ b/frontend/src/components/pages/Contact.jsx @@ -3,7 +3,7 @@ import { useNavigate } from 'react-router-dom'; import { API_URL } from '../../constants'; -function Contact({ user }) { +function Contact({ user, setLoggedIn, setUser }) { const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [phone, setPhone] = useState(""); @@ -58,6 +58,9 @@ function Contact({ user }) { } } catch (error) { setErrorMessages("An error occurred. Please try again."); + setLoggedIn(false); + setUser(null); + localStorage.removeItem('jwt'); } }; diff --git a/frontend/src/components/pages/Kits.jsx b/frontend/src/components/pages/Kits.jsx index 117edf21..b7155e45 100644 --- a/frontend/src/components/pages/Kits.jsx +++ b/frontend/src/components/pages/Kits.jsx @@ -22,7 +22,6 @@ function Kits({user, setUser}) { const response = await fetch(kitsUrl); if (response.ok) { const json = await response.json(); - console.log("Fetched kits data:", json); setKits(json); } else { throw response; @@ -30,6 +29,9 @@ function Kits({user, setUser}) { } catch (e) { setError("An error occurred."); console.log("An error occurred", e); + setLoggedIn(false); + setUser(null); + localStorage.removeItem('jwt'); } finally { setLoading(false); } @@ -79,13 +81,9 @@ function Kits({user, setUser}) { {error &&

{error}

}
- {kits.map( - ( - kit // Map through the list of kits - ) => ( - <> -
-
+ {kits.map(kit => ( +
- Admin Dashboard + Admin Dashboard
@@ -280,7 +289,7 @@ const AdminDashboard = ({ user }) => {
- +
@@ -312,6 +321,8 @@ const AdminDashboard = ({ user }) => { headers={headers.userUrl} apiEndpoint={userUrl} handleShow={(item) => handleShow(item, "user")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === kitsUrl && ( @@ -319,6 +330,8 @@ const AdminDashboard = ({ user }) => { headers={headers.kitsUrl} apiEndpoint={kitsUrl} handleShow={(item) => handleShow(item, "kit")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === kitItemsUrl && ( @@ -326,6 +339,8 @@ const AdminDashboard = ({ user }) => { headers={headers.kitItemsUrl} apiEndpoint={kitItemsUrl} handleShow={(item) => handleShow(item, "kitItem")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === ordersUrl && ( @@ -335,6 +350,8 @@ const AdminDashboard = ({ user }) => { handleShow={(item) => handleShow(item, "order") } + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === donationUrl && ( @@ -342,6 +359,8 @@ const AdminDashboard = ({ user }) => { headers={headers.donationUrl} apiEndpoint={donationUrl} handleShow={(item) => handleShow(item, "donation")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === contactsUrl && ( @@ -349,6 +368,8 @@ const AdminDashboard = ({ user }) => { headers={headers.contactsUrl} apiEndpoint={contactsUrl} handleShow={(item) => handleShow(item, "contact")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} diff --git a/frontend/src/components/pages/Contact.jsx b/frontend/src/components/pages/Contact.jsx index 41f1f012..d20b4228 100644 --- a/frontend/src/components/pages/Contact.jsx +++ b/frontend/src/components/pages/Contact.jsx @@ -3,7 +3,7 @@ import { useNavigate } from 'react-router-dom'; import { API_URL } from '../../constants'; -function Contact({ user }) { +function Contact({ user, setLoggedIn, setUser }) { const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [phone, setPhone] = useState(""); @@ -58,6 +58,9 @@ function Contact({ user }) { } } catch (error) { setErrorMessages("An error occurred. Please try again."); + setLoggedIn(false); + setUser(null); + localStorage.removeItem('jwt'); } }; diff --git a/frontend/src/components/pages/Kits.jsx b/frontend/src/components/pages/Kits.jsx index 1cadc79a..b40254d4 100644 --- a/frontend/src/components/pages/Kits.jsx +++ b/frontend/src/components/pages/Kits.jsx @@ -21,7 +21,6 @@ function Kits({user}) { const response = await fetch(kitsUrl); if (response.ok) { const json = await response.json(); - console.log("Fetched kits data:", json); setKits(json); } else { throw response; @@ -29,6 +28,9 @@ function Kits({user}) { } catch (e) { setError("An error occurred."); console.log("An error occurred", e); + setLoggedIn(false); + setUser(null); + localStorage.removeItem('jwt'); } finally { setLoading(false); } @@ -78,13 +80,9 @@ function Kits({user}) { {error &&

{error}

}
- {kits.map( - ( - kit // Map through the list of kits - ) => ( - <> -
-
+ {kits.map(kit => ( +
- Admin Dashboard + Admin Dashboard
@@ -280,7 +289,7 @@ const AdminDashboard = ({ user }) => {
- +
@@ -312,6 +321,8 @@ const AdminDashboard = ({ user }) => { headers={headers.userUrl} apiEndpoint={userUrl} handleShow={(item) => handleShow(item, "user")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === kitsUrl && ( @@ -319,6 +330,8 @@ const AdminDashboard = ({ user }) => { headers={headers.kitsUrl} apiEndpoint={kitsUrl} handleShow={(item) => handleShow(item, "kit")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === kitItemsUrl && ( @@ -326,6 +339,8 @@ const AdminDashboard = ({ user }) => { headers={headers.kitItemsUrl} apiEndpoint={kitItemsUrl} handleShow={(item) => handleShow(item, "kitItem")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === ordersUrl && ( @@ -335,6 +350,8 @@ const AdminDashboard = ({ user }) => { handleShow={(item) => handleShow(item, "order") } + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === donationUrl && ( @@ -342,6 +359,8 @@ const AdminDashboard = ({ user }) => { headers={headers.donationUrl} apiEndpoint={donationUrl} handleShow={(item) => handleShow(item, "donation")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === contactsUrl && ( @@ -349,6 +368,8 @@ const AdminDashboard = ({ user }) => { headers={headers.contactsUrl} apiEndpoint={contactsUrl} handleShow={(item) => handleShow(item, "contact")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} diff --git a/frontend/src/components/pages/Contact.jsx b/frontend/src/components/pages/Contact.jsx index 1118910e..1c5390d9 100644 --- a/frontend/src/components/pages/Contact.jsx +++ b/frontend/src/components/pages/Contact.jsx @@ -3,7 +3,7 @@ import { useNavigate } from 'react-router-dom'; import { API_URL } from '../../constants'; -function Contact({ user }) { +function Contact({ user, setLoggedIn, setUser }) { const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [phone, setPhone] = useState(""); @@ -52,6 +52,9 @@ function Contact({ user }) { } } catch (error) { setErrorMessages("An error occurred. Please try again."); + setLoggedIn(false); + setUser(null); + localStorage.removeItem('jwt'); } }; diff --git a/frontend/src/components/pages/Kits.jsx b/frontend/src/components/pages/Kits.jsx index a9791132..765b0c54 100644 --- a/frontend/src/components/pages/Kits.jsx +++ b/frontend/src/components/pages/Kits.jsx @@ -21,7 +21,6 @@ function Kits({user}) { const response = await fetch(kitsUrl); if (response.ok) { const json = await response.json(); - console.log("Fetched kits data:", json); setKits(json); } else { throw response; @@ -29,6 +28,9 @@ function Kits({user}) { } catch (e) { setError("An error occurred."); console.log("An error occurred", e); + setLoggedIn(false); + setUser(null); + localStorage.removeItem('jwt'); } finally { setLoading(false); } @@ -64,13 +66,9 @@ function Kits({user}) { {error &&

{error}

}
- {kits.map( - ( - kit // Map through the list of kits - ) => ( - <> -
-
+ {kits.map(kit => ( +
- Admin Dashboard + Admin Dashboard
@@ -280,7 +289,7 @@ const AdminDashboard = ({ user }) => {
- +
@@ -312,6 +321,8 @@ const AdminDashboard = ({ user }) => { headers={headers.userUrl} apiEndpoint={userUrl} handleShow={(item) => handleShow(item, "user")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === kitsUrl && ( @@ -319,6 +330,8 @@ const AdminDashboard = ({ user }) => { headers={headers.kitsUrl} apiEndpoint={kitsUrl} handleShow={(item) => handleShow(item, "kit")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === kitItemsUrl && ( @@ -326,6 +339,8 @@ const AdminDashboard = ({ user }) => { headers={headers.kitItemsUrl} apiEndpoint={kitItemsUrl} handleShow={(item) => handleShow(item, "kitItem")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === ordersUrl && ( @@ -335,6 +350,8 @@ const AdminDashboard = ({ user }) => { handleShow={(item) => handleShow(item, "order") } + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === donationUrl && ( @@ -342,6 +359,8 @@ const AdminDashboard = ({ user }) => { headers={headers.donationUrl} apiEndpoint={donationUrl} handleShow={(item) => handleShow(item, "donation")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} {selectedEndpoint === contactsUrl && ( @@ -349,6 +368,8 @@ const AdminDashboard = ({ user }) => { headers={headers.contactsUrl} apiEndpoint={contactsUrl} handleShow={(item) => handleShow(item, "contact")} + setLoggedIn={setLoggedIn} + setUser={setUser} /> )} diff --git a/frontend/src/components/pages/Contact.jsx b/frontend/src/components/pages/Contact.jsx index 1118910e..1c5390d9 100644 --- a/frontend/src/components/pages/Contact.jsx +++ b/frontend/src/components/pages/Contact.jsx @@ -3,7 +3,7 @@ import { useNavigate } from 'react-router-dom'; import { API_URL } from '../../constants'; -function Contact({ user }) { +function Contact({ user, setLoggedIn, setUser }) { const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [phone, setPhone] = useState(""); @@ -52,6 +52,9 @@ function Contact({ user }) { } } catch (error) { setErrorMessages("An error occurred. Please try again."); + setLoggedIn(false); + setUser(null); + localStorage.removeItem('jwt'); } }; diff --git a/frontend/src/components/pages/Kits.jsx b/frontend/src/components/pages/Kits.jsx index 169ab0bc..fdd0af87 100644 --- a/frontend/src/components/pages/Kits.jsx +++ b/frontend/src/components/pages/Kits.jsx @@ -21,7 +21,6 @@ function Kits({user}) { const response = await fetch(kitsUrl); if (response.ok) { const json = await response.json(); - console.log("Fetched kits data:", json); setKits(json); } else { throw response; @@ -29,6 +28,9 @@ function Kits({user}) { } catch (e) { setError("An error occurred."); console.log("An error occurred", e); + setLoggedIn(false); + setUser(null); + localStorage.removeItem('jwt'); } finally { setLoading(false); } @@ -64,13 +66,9 @@ function Kits({user}) { {error &&

{error}

}
- {kits.map( - ( - kit // Map through the list of kits - ) => ( - <> -
-
+ {kits.map(kit => ( +