refactoring, check auth

This commit is contained in:
2025-07-05 19:48:05 +05:00
parent e21a51482a
commit 12f7ea8d1c
8 changed files with 440 additions and 267 deletions

View File

@ -1,13 +1,78 @@
import { MemoryRouter as Router, Routes, Route } from 'react-router-dom';
import {
MemoryRouter as Router,
Routes,
Route,
Navigate,
} from 'react-router-dom';
import Login from './pages/Login';
import Dashboard from './pages/Dashboard';
import { ReactNode, useEffect, useState } from 'react';
import './App.css';
export default function App() {
const AuthCheck = ({ children }: { children: ReactNode }) => {
const [isAuthenticated, setIsAuthenticated] = useState<boolean | null>(null);
useEffect(() => {
const checkAuth = async () => {
try {
const savedConfig = localStorage.getItem('launcher_config');
if (savedConfig) {
const config = JSON.parse(savedConfig);
if (config.accessToken) {
// Можно добавить дополнительную проверку токена
const isValid = await validateToken(config.accessToken);
setIsAuthenticated(isValid);
return;
}
}
setIsAuthenticated(false);
} catch (error) {
console.error('Ошибка проверки авторизации:', error);
setIsAuthenticated(false);
}
};
checkAuth();
}, []);
const validateToken = async (token: string) => {
try {
const response = await fetch('https://authserver.ely.by/auth/validate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ accessToken: token }),
});
return response.ok;
} catch (error) {
return false;
}
};
if (isAuthenticated === null) {
return <div>Loading...</div>;
}
return isAuthenticated ? children : <Navigate to="/login" replace />;
};
const App = () => {
return (
<Router>
<Routes>
<Route path="/" element={<Login />} />
<Route path="/login" element={<Login />} />
<Route
path="/"
element={
<AuthCheck>
<Dashboard />
</AuthCheck>
}
/>
</Routes>
</Router>
);
}
};
export default App;