Files
sfera/src/components/auth-guard.tsx
Veronika Smirnova 10af6f08cc Обновления системы после анализа и оптимизации архитектуры
- Обновлена схема Prisma с новыми полями и связями
- Актуализированы правила системы в rules-complete.md
- Оптимизированы GraphQL типы, запросы и мутации
- Улучшены компоненты интерфейса и валидация данных
- Исправлены критические ESLint ошибки: удалены неиспользуемые импорты и переменные
- Добавлены тестовые файлы для проверки функционала

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-06 23:44:49 +03:00

54 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use client'
import { useEffect, useState, useRef } from 'react'
import { useAuth } from '@/hooks/useAuth'
import { AuthFlow } from './auth/auth-flow'
interface AuthGuardProps {
children: React.ReactNode
fallback?: React.ReactNode
}
export function AuthGuard({ children, fallback }: AuthGuardProps) {
const { isAuthenticated, isLoading, checkAuth, user } = useAuth()
const [isChecking, setIsChecking] = useState(true)
const initRef = useRef(false) // Защита от повторных инициализаций
useEffect(() => {
const initAuth = async () => {
if (initRef.current) {
return
}
initRef.current = true
await checkAuth()
setIsChecking(false)
}
initAuth()
}, [checkAuth, isAuthenticated, user]) // Добавляем зависимости как требует линтер
// Показываем лоадер пока проверяем авторизацию
if (isChecking || isLoading) {
return (
<div className="min-h-screen flex items-center justify-center">
<div className="text-center text-white">
<div className="animate-spin rounded-full h-16 w-16 border-4 border-white border-t-transparent mx-auto mb-4"></div>
<p className="text-white/80">Проверяем авторизацию...</p>
</div>
</div>
)
}
// Если не авторизован, показываем форму авторизации
if (!isAuthenticated) {
return fallback || <AuthFlow />
}
// Если авторизован, показываем защищенный контент
return <>{children}</>
}