source: imaps-frontend/src/App.jsx

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.3 KB
Line 
1// eslint-disable-next-line no-unused-vars
2import React, { useState, useEffect } from "react";
3import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
4import FinkiMaps from "./pages/FinkiMaps/FinkiMaps";
5import Maps from "./pages/Maps/Maps";
6import LoginPage from "./pages/Login/Login";
7import Signup from "./pages/Signup/Signup";
8import IMaps from "./pages/IMaps/IMaps";
9import Draw from "./pages/Draw/Draw";
10import Error from "./pages/Error/Error";
11import ProtectedRoute from "./components/ProtectedRoute/ProtectedRoute";
12import "./App.css";
13import HttpService from "./scripts/net/HttpService";
14
15function App() {
16 const [isAuthenticated, setIsAuthenticated] = useState(false);
17 const [loading, setLoading] = useState(true);
18
19 useEffect(() => {
20 const token = localStorage.getItem("token");
21 const httpService = new HttpService("http://localhost:8080/api/auth");
22
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 };
37
38 if (token) {
39 verifyToken();
40 //setLoading(false);
41 } else {
42 setIsAuthenticated(false);
43 setLoading(false);
44 }
45 }, []);
46
47 const handleLogin = (token) => {
48 localStorage.setItem("token", token);
49 setIsAuthenticated(true);
50 };
51
52 if (loading) {
53 return (
54 <div className="loading-container">
55 <div className="spinner"></div>
56 <p>Loading, please wait...</p>
57 </div>
58 );
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}
79
80export default App;
Note: See TracBrowser for help on using the repository browser.