mirror of
https://github.com/lorsanstand/Aether.git
synced 2026-06-19 12:05:16 +03:00
100 lines
3.0 KiB
TypeScript
100 lines
3.0 KiB
TypeScript
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
|
|
import { useEffect } from 'react';
|
|
import { useAuthStore } from './store/authStore';
|
|
import { useThemeStore } from './store/themeStore';
|
|
import { authService } from './services/authService';
|
|
import AuthPage from './pages/AuthPage';
|
|
import VerifyEmailPage from './pages/VerifyEmailPage';
|
|
import ResetPasswordPage from './pages/ResetPasswordPage';
|
|
import ForgotPasswordPage from './pages/ForgotPasswordPage';
|
|
import ChatPage from './pages/ChatPage';
|
|
import ProfilePage from './pages/ProfilePage';
|
|
import SettingsPage from './pages/SettingsPage';
|
|
|
|
function PrivateRoute({ children }: { children: React.ReactNode }) {
|
|
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
|
|
const isLoading = useAuthStore((state) => state.isLoading);
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<div className="min-h-screen flex items-center justify-center" style={{ backgroundColor: '#F5F5F1' }}>
|
|
<div className="w-16 h-16 border-4 border-gray-200 rounded-full animate-spin" style={{ borderTopColor: '#6B705C' }}></div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return isAuthenticated ? <>{children}</> : <Navigate to="/auth" />;
|
|
}
|
|
|
|
function App() {
|
|
const setUser = useAuthStore((state) => state.setUser);
|
|
const setLoading = useAuthStore((state) => state.setLoading);
|
|
const theme = useThemeStore((state) => state.theme);
|
|
|
|
useEffect(() => {
|
|
// Apply theme on mount
|
|
document.documentElement.setAttribute('data-theme', theme);
|
|
}, [theme]);
|
|
|
|
useEffect(() => {
|
|
const checkAuth = async () => {
|
|
try {
|
|
const user = await authService.getCurrentUser();
|
|
setUser(user);
|
|
} catch (error) {
|
|
setUser(null);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
checkAuth();
|
|
}, []); // Пустой массив зависимостей - выполнится только один раз
|
|
|
|
return (
|
|
<BrowserRouter>
|
|
<Routes>
|
|
<Route path="/auth" element={<AuthPage />} />
|
|
<Route path="/verify-email/:token" element={<VerifyEmailPage />} />
|
|
<Route path="/reset-password/:token" element={<ResetPasswordPage />} />
|
|
<Route path="/forgot-password" element={<ForgotPasswordPage />} />
|
|
<Route
|
|
path="/chat"
|
|
element={
|
|
<PrivateRoute>
|
|
<ChatPage />
|
|
</PrivateRoute>
|
|
}
|
|
/>
|
|
<Route
|
|
path="/chat/:chatId"
|
|
element={
|
|
<PrivateRoute>
|
|
<ChatPage />
|
|
</PrivateRoute>
|
|
}
|
|
/>
|
|
<Route
|
|
path="/profile"
|
|
element={
|
|
<PrivateRoute>
|
|
<ProfilePage />
|
|
</PrivateRoute>
|
|
}
|
|
/>
|
|
<Route
|
|
path="/settings"
|
|
element={
|
|
<PrivateRoute>
|
|
<SettingsPage />
|
|
</PrivateRoute>
|
|
}
|
|
/>
|
|
<Route path="/" element={<Navigate to="/chat" />} />
|
|
</Routes>
|
|
</BrowserRouter>
|
|
);
|
|
}
|
|
|
|
export default App;
|