68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
import { Suspense, useEffect } from 'react';
|
|
import { RouterProvider } from 'react-router-dom';
|
|
import { Toaster } from 'react-hot-toast';
|
|
import axios from 'axios';
|
|
import { router } from './routes/adminRoutes';
|
|
import { useAdminAuthStore } from './store/adminAuthStore';
|
|
import { BASE_DOMAIN } from './services/api';
|
|
|
|
function SuspenseFallback() {
|
|
return (
|
|
<div className="min-h-screen flex items-center justify-center bg-gray-50 font-vazir" dir="rtl">
|
|
<div className="flex flex-col items-center gap-3">
|
|
<div className="w-10 h-10 border-4 border-purple-600 border-t-transparent rounded-full animate-spin"></div>
|
|
<span className="text-sm font-bold text-gray-500">در حال بارگذاری ماژول ادمین...</span>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function App() {
|
|
const isAuthenticated = useAdminAuthStore((state) => state.isAuthenticated);
|
|
const setAuth = useAdminAuthStore((state) => state.setAuth);
|
|
|
|
// Proactive token refresh every 10 minutes to prevent 15-min JWT session loss during form edits (A1)
|
|
useEffect(() => {
|
|
if (!isAuthenticated) return;
|
|
|
|
const refreshSession = async () => {
|
|
try {
|
|
const res = await axios.post(
|
|
`${BASE_DOMAIN}/api/auth/refresh`,
|
|
{},
|
|
{ withCredentials: true }
|
|
);
|
|
if (res.data?.success && res.data.data?.accessToken) {
|
|
setAuth(res.data.data.accessToken, res.data.data.user);
|
|
}
|
|
} catch {
|
|
// Silently catch; let api interceptor handle on next actual request if needed
|
|
}
|
|
};
|
|
|
|
// Refresh every 10 minutes (600,000 ms)
|
|
const interval = setInterval(refreshSession, 10 * 60 * 1000);
|
|
return () => clearInterval(interval);
|
|
}, [isAuthenticated, setAuth]);
|
|
|
|
return (
|
|
<>
|
|
<Toaster
|
|
position="top-center"
|
|
toastOptions={{
|
|
className: 'font-vazir text-sm font-bold',
|
|
style: {
|
|
direction: 'rtl',
|
|
fontFamily: 'Vazirmatn, system-ui, sans-serif'
|
|
}
|
|
}}
|
|
/>
|
|
<Suspense fallback={<SuspenseFallback />}>
|
|
<RouterProvider router={router} />
|
|
</Suspense>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default App;
|