"use client" import { useEffect, useState } from "react" import { useRouter } from "next/navigation" import { useAuth } from "@/context/auth-context" import { DashboardStatsOverview } from "@/components/dashboard-stats" import { Loader2, AlertCircle } from "lucide-react" import { Button } from "@/components/ui/button" export default function DashboardPage() { const { isLoadingAuth, walletConnected, walletAddress, firebaseUser, authMethod } = useAuth() const router = useRouter() const [showDashboard, setShowDashboard] = useState(false) const [debugInfo, setDebugInfo] = useState(null) useEffect(() => { // Debug authentication state const authState = { isLoadingAuth, walletConnected, walletAddress: !!walletAddress, firebaseUser: !!firebaseUser, authMethod, localStorage: { token: !!localStorage.getItem('openlearnx_jwt_token'), wallet: !!localStorage.getItem('openlearnx_wallet'), user: !!localStorage.getItem('openlearnx_user') } } setDebugInfo(authState) console.log('📊 Dashboard auth state:', authState) // Give auth some time to initialize const timer = setTimeout(() => { const isAuthenticated = (walletConnected && walletAddress) || firebaseUser if (isAuthenticated) { console.log('✅ User authenticated, showing dashboard') setShowDashboard(true) } else if (!isLoadingAuth) { console.log('❌ User not authenticated, redirecting to login') router.replace("/auth/login") } }, 2000) // Wait 2 seconds for auth to stabilize return () => clearTimeout(timer) }, [isLoadingAuth, walletConnected, walletAddress, firebaseUser, authMethod, router]) // Show loading state if (isLoadingAuth || !showDashboard) { return (

Loading Dashboard...

{walletConnected ? `Connected to ${walletAddress?.slice(0, 6)}...${walletAddress?.slice(-4)}` : firebaseUser ? `Logged in as ${firebaseUser.email}` : 'Verifying authentication...'}

{/* Debug info in development */} {process.env.NODE_ENV === 'development' && debugInfo && (
Debug Info
{JSON.stringify(debugInfo, null, 2)}
)}
) } // Show error state if no auth after loading if (!walletConnected && !firebaseUser && !isLoadingAuth) { return (

Authentication Required

Please log in to access your dashboard.

{/* Debug info */} {process.env.NODE_ENV === 'development' && (
Debug Info
{JSON.stringify(debugInfo, null, 2)}
)}
) } // Show dashboard if authenticated return }