File size: 2,022 Bytes
7781557 3bb94b1 7781557 3bb94b1 7781557 3bb94b1 7781557 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
import React, { useEffect } from 'react';
import { BrowserRouter as Router, Route, Routes, Navigate, useLocation} from 'react-router-dom';
import Login from './components/Login';
import Register from './components/Register';
import Chat from './components/Chat';
import Opportunities from './components/Opportunities';
import { AuthProvider, useAuth } from './services/AuthContext';
const PrivateRoute = ({ children }) => {
const { token, loading } = useAuth();
const location = useLocation();
if (loading) {
// Show loading spinner or placeholder
return <div className="flex items-center justify-center h-screen">Loading...</div>;
}
return token ? children : <Navigate to="/login" state={{ from: location }} replace />;
};
const PublicRoute = ({ children }) => {
const { token, loading } = useAuth();
const location = useLocation();
if (loading) {
return <div className="flex items-center justify-center h-screen">Loading...</div>;
}
return !token ? children : <Navigate to="/" replace />;
};
function AppRoutes() {
const location = useLocation();
useEffect(() => {
// Log the current route for debugging
console.log('Current route:', location.pathname);
}, [location]);
return (
<>
<Routes>
<Route
path="/login"
element={
<PublicRoute>
<Login />
</PublicRoute>
}
/>
<Route
path="/register"
element={
<PublicRoute>
<Register />
</PublicRoute>
}
/>
<Route
path="/"
element={
<PrivateRoute>
<Opportunities/>
</PrivateRoute>
}
/>
{/* Catch all route */}
<Route path="*" element={<Navigate to="/" replace />} />
</Routes>
</>
);
}
function App() {
return (
<Router>
<AuthProvider>
<AppRoutes/>
</AuthProvider>
</Router>
);
}
export default App; |