This commit is contained in:
2025-05-21 01:34:02 +03:00
parent 1d64811880
commit d3a760b6ba
25 changed files with 1839 additions and 550 deletions

View File

@@ -1,5 +1,5 @@
import { Component, Show, Suspense, createSignal, lazy, onMount } from 'solid-js'
import { isAuthenticated } from './auth'
import { Component, Show, Suspense, createSignal, lazy, onMount, createEffect } from 'solid-js'
import { isAuthenticated, getAuthTokenFromCookie } from './auth'
// Ленивая загрузка компонентов
const AdminPage = lazy(() => import('./admin'))
@@ -11,14 +11,58 @@ const LoginPage = lazy(() => import('./login'))
const App: Component = () => {
const [authenticated, setAuthenticated] = createSignal<boolean | null>(null)
const [loading, setLoading] = createSignal(true)
const [checkingAuth, setCheckingAuth] = createSignal(true)
// Проверяем авторизацию при монтировании
onMount(() => {
const authed = isAuthenticated()
setAuthenticated(authed)
setLoading(false)
checkAuthentication()
})
// Периодическая проверка авторизации
createEffect(() => {
const authCheckInterval = setInterval(() => {
// Перепроверяем статус авторизации каждые 60 секунд
if (!checkingAuth()) {
const authed = isAuthenticated()
if (!authed && authenticated()) {
console.log('Сессия истекла, требуется повторная авторизация')
setAuthenticated(false)
}
}
}, 60000)
return () => clearInterval(authCheckInterval)
})
// Функция проверки авторизации
const checkAuthentication = async () => {
setCheckingAuth(true)
setLoading(true)
try {
// Проверяем состояние авторизации
const authed = isAuthenticated()
// Если токен есть, но он невалидный, авторизация не удалась
if (authed) {
const token = getAuthTokenFromCookie() || localStorage.getItem('auth_token')
if (!token || token.length < 10) {
setAuthenticated(false)
} else {
setAuthenticated(true)
}
} else {
setAuthenticated(false)
}
} catch (error) {
console.error('Ошибка при проверке авторизации:', error)
setAuthenticated(false)
} finally {
setLoading(false)
setCheckingAuth(false)
}
}
// Обработчик успешной авторизации
const handleLoginSuccess = () => {
setAuthenticated(true)
@@ -35,7 +79,7 @@ const App: Component = () => {
fallback={
<div class="loading-screen">
<div class="loading-spinner" />
<h2>Загрузка...</h2>
<h2>Загрузка компонентов...</h2>
</div>
}
>
@@ -44,12 +88,12 @@ const App: Component = () => {
fallback={
<div class="loading-screen">
<div class="loading-spinner" />
<h2>Загрузка...</h2>
<h2>Проверка авторизации...</h2>
</div>
}
>
{authenticated() ? (
<AdminPage onLogout={handleLogout} />
<AdminPage apiUrl={`${location.origin}/graphql`} onLogout={handleLogout} />
) : (
<LoginPage onLoginSuccess={handleLoginSuccess} />
)}