oauth2
This commit is contained in:
@@ -1,16 +1,73 @@
|
||||
import { BrowserRouter, Routes, Route } from 'react-router-dom';
|
||||
import Home from './pages/Home';
|
||||
import { useEffect } from 'react';
|
||||
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
|
||||
import Login from './pages/Login';
|
||||
import FamilyView from './pages/FamilyView';
|
||||
import AdminPanel from './pages/AdminPanel';
|
||||
import NoFamily from './pages/NoFamily';
|
||||
import { useStore } from './store/useStore';
|
||||
import { authApi } from './api/client';
|
||||
import { Loader2 } from 'lucide-react';
|
||||
|
||||
function AppContent() {
|
||||
const { user, isAuthenticated, isLoading, setUser, setIsLoading } = useStore();
|
||||
|
||||
useEffect(() => {
|
||||
checkAuth();
|
||||
}, []);
|
||||
|
||||
const checkAuth = async () => {
|
||||
try {
|
||||
const response = await authApi.me();
|
||||
setUser(response.data);
|
||||
} catch {
|
||||
setUser(null);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center gradient-bg">
|
||||
<div className="flex items-center gap-3 text-white">
|
||||
<Loader2 className="w-8 h-8 animate-spin" />
|
||||
<span className="text-xl font-medium">Загрузка...</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return (
|
||||
<Routes>
|
||||
<Route path="*" element={<Login />} />
|
||||
</Routes>
|
||||
);
|
||||
}
|
||||
|
||||
if (!user?.family_id) {
|
||||
return (
|
||||
<Routes>
|
||||
<Route path="/adminpanel" element={<AdminPanel />} />
|
||||
<Route path="*" element={<NoFamily />} />
|
||||
</Routes>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Routes>
|
||||
<Route path="/" element={<Navigate to={`/family/${user.family_id}`} replace />} />
|
||||
<Route path="/family/:familyId" element={<FamilyView />} />
|
||||
<Route path="/adminpanel" element={<AdminPanel />} />
|
||||
<Route path="*" element={<Navigate to={`/family/${user.family_id}`} replace />} />
|
||||
</Routes>
|
||||
);
|
||||
}
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<BrowserRouter>
|
||||
<Routes>
|
||||
<Route path="/" element={<Home />} />
|
||||
<Route path="/family/:familyId" element={<FamilyView />} />
|
||||
<Route path="/adminpanel" element={<AdminPanel />} />
|
||||
</Routes>
|
||||
<AppContent />
|
||||
</BrowserRouter>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user