-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathApp.tsx
More file actions
66 lines (56 loc) · 2.45 KB
/
App.tsx
File metadata and controls
66 lines (56 loc) · 2.45 KB
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
import React from 'react';
import { HashRouter, Navigate, Route, Routes } from 'react-router-dom';
import { Layout } from './components/layout/Layout';
import { ThemeWrapper } from './components/layout/ThemeWrapper';
import { AuthProvider, useAuth } from './contexts/AuthContext';
import { ThemeProvider } from './contexts/ThemeContext';
import { ToastProvider } from './contexts/ToastContext';
import { ToastContainer } from './components/ui/Toast';
import { ErrorBoundary } from './components/ErrorBoundary';
import { Auth } from './pages/Auth';
import { Dashboard } from './pages/Dashboard';
import { Friends } from './pages/Friends';
import { GroupDetails } from './pages/GroupDetails';
import { Groups } from './pages/Groups';
import { Profile } from './pages/Profile';
// Protected Route Wrapper
const ProtectedRoute = ({ children }: { children: React.ReactElement }) => {
const { isAuthenticated, isLoading } = useAuth();
if (isLoading) return <div className="h-screen w-full flex items-center justify-center">Loading...</div>;
if (!isAuthenticated) {
return <Navigate to="/login" replace />;
}
return <Layout>{children}</Layout>;
};
const AppRoutes = () => {
const { isAuthenticated } = useAuth();
return (
<Routes>
<Route path="/login" element={!isAuthenticated ? <ThemeWrapper><Auth /></ThemeWrapper> : <Navigate to="/dashboard" />} />
<Route path="/signup" element={!isAuthenticated ? <ThemeWrapper><Auth /></ThemeWrapper> : <Navigate to="/dashboard" />} />
<Route path="/dashboard" element={<ProtectedRoute><Dashboard /></ProtectedRoute>} />
<Route path="/groups" element={<ProtectedRoute><Groups /></ProtectedRoute>} />
<Route path="/groups/:id" element={<ProtectedRoute><GroupDetails /></ProtectedRoute>} />
<Route path="/friends" element={<ProtectedRoute><Friends /></ProtectedRoute>} />
<Route path="/profile" element={<ProtectedRoute><Profile /></ProtectedRoute>} />
<Route path="*" element={<Navigate to={isAuthenticated ? "/dashboard" : "/login"} />} />
</Routes>
);
}
const App = () => {
return (
<ThemeProvider>
<ToastProvider>
<AuthProvider>
<HashRouter>
<ErrorBoundary>
<AppRoutes />
<ToastContainer />
</ErrorBoundary>
</HashRouter>
</AuthProvider>
</ToastProvider>
</ThemeProvider>
);
};
export default App;