Changeset 0c6b92a for imaps-frontend/src/App.jsx
- Timestamp:
- 12/12/24 17:06:06 (5 weeks ago)
- Branches:
- main
- Parents:
- d565449
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
imaps-frontend/src/App.jsx
rd565449 r0c6b92a 1 1 // eslint-disable-next-line no-unused-vars 2 import React, { useState, useEffect } from "react"; 3 import { BrowserRouter as Router, Routes, Route } from "react-router-dom"; 4 import FinkiMaps from "./pages/FinkiMaps/FinkiMaps"; 5 import Maps from "./pages/Maps/Maps"; 2 import React from "react"; 3 import {BrowserRouter as Router, Route, Routes} from "react-router-dom"; 4 import BrowseMaps from "./pages/BrowseMaps/BrowseMaps.jsx"; 6 5 import LoginPage from "./pages/Login/Login"; 7 6 import Signup from "./pages/Signup/Signup"; … … 11 10 import ProtectedRoute from "./components/ProtectedRoute/ProtectedRoute"; 12 11 import "./App.css"; 13 import HttpService from "./scripts/net/HttpService"; 12 import MapView from "./pages/MapView/MapView.jsx"; 13 import MyMaps from "./pages/MyMaps/MyMaps.jsx"; 14 15 import '@fontsource/roboto/300.css'; 16 import '@fontsource/roboto/400.css'; 17 import '@fontsource/roboto/500.css'; 18 import '@fontsource/roboto/700.css'; 19 import {AppProvider} from "./components/AppContext/AppContext.jsx"; 20 import AdminPage from "./pages/AdminPage/AdminPage.jsx"; 21 14 22 15 23 function App() { 16 const [isAuthenticated, setIsAuthenticated] = useState(false);17 const [loading, setLoading] = useState(true);18 24 19 useEffect(() => { 20 const token = localStorage.getItem("token"); 21 const httpService = new HttpService("http://localhost:8080/api/auth"); 25 return ( 22 26 23 const verifyToken = async () => { 24 try { 25 const response = await httpService.get(`/verify?token=${token}`); 26 if (response.username) { 27 setIsAuthenticated(true); 28 console.log("good"); 29 } 30 } catch (error) { 31 console.log("ERROR: ", error); 32 setIsAuthenticated(false); 33 } finally { 34 setLoading(false); 35 } 36 }; 27 <AppProvider> 28 <Router> 29 <Routes> 37 30 38 if (token) { 39 verifyToken(); 40 //setLoading(false); 41 } else { 42 setIsAuthenticated(false); 43 setLoading(false); 44 } 45 }, []); 31 <Route element={<ProtectedRoute/>}> 32 <Route path="/myMaps/:mapName/Draw" element={<Draw/>}/> 33 <Route path="/myMaps" element={<MyMaps/>}/> 34 <Route path="/myMaps/:mapName/View" element={<MapView isPrivate={true}/>}/> 35 <Route path="/Admin" element={<AdminPage/>}/> 36 </Route> 37 <Route path="/" element={<IMaps/>}/> 38 <Route path="/Maps/:mapName/View" element={<MapView isPrivate={false}/>}/> 39 <Route path="/Maps" element={<BrowseMaps/>}/> 46 40 47 const handleLogin = (token) => {48 localStorage.setItem("token", token);49 setIsAuthenticated(true);50 };51 41 52 if (loading) { 53 return ( 54 <div className="loading-container"> 55 <div className="spinner"></div> 56 <p>Loading, please wait...</p> 57 </div> 42 <Route path="/Login" element={<LoginPage/>}/> 43 <Route path="/Signup" element={<Signup/>}/> 44 45 <Route path="*" element={<Error/>}/> 46 </Routes> 47 </Router> 48 </AppProvider> 49 50 58 51 ); 59 }60 61 return (62 <Router>63 <Routes>64 <Route path="/" element={<IMaps />} />65 <Route path="/Maps/FinkiMaps/View" element={<FinkiMaps />} />66 <Route path="/Maps" element={<Maps />} />67 <Route path="/Login" element={<LoginPage onLogin={handleLogin} />} />68 <Route path="/Signup" element={<Signup />} />69 70 <Route element={<ProtectedRoute isAuthenticated={isAuthenticated} />}>71 <Route path="/Maps/FinkiMaps/Draw" element={<Draw />} />72 </Route>73 74 <Route path="*" element={<Error />} />75 </Routes>76 </Router>77 );78 52 } 79 53
Note:
See TracChangeset
for help on using the changeset viewer.