[d565449] | 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";
|
---|
| 6 | import LoginPage from "./pages/Login/Login";
|
---|
| 7 | import Signup from "./pages/Signup/Signup";
|
---|
| 8 | import IMaps from "./pages/IMaps/IMaps";
|
---|
| 9 | import Draw from "./pages/Draw/Draw";
|
---|
| 10 | import Error from "./pages/Error/Error";
|
---|
| 11 | import ProtectedRoute from "./components/ProtectedRoute/ProtectedRoute";
|
---|
| 12 | import "./App.css";
|
---|
| 13 | import HttpService from "./scripts/net/HttpService";
|
---|
| 14 |
|
---|
| 15 | function 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 |
|
---|
| 80 | export default App;
|
---|