'use client' import { useState, useEffect } from 'react' import AuthModal from './AuthModal' interface User { id: number username: string email: string } export default function Nav() { const [user, setUser] = useState(null) const [showAuthModal, setShowAuthModal] = useState(false) const [loading, setLoading] = useState(true) useEffect(() => { checkAuth() }, []) const checkAuth = async () => { try { const response = await fetch('/api/auth/session', { credentials: 'include', }) if (response.ok) { const data = await response.json() setUser(data.user) } } catch (error) { console.error('Error checking auth:', error) } finally { setLoading(false) } } const handleLogin = (loggedInUser: User) => { setUser(loggedInUser) setShowAuthModal(false) } const handleLogout = async () => { try { await fetch('/api/auth/logout', { method: 'POST', credentials: 'include', }) setUser(null) } catch (error) { console.error('Error logging out:', error) } } return ( <> setShowAuthModal(false)} onLogin={handleLogin} /> ) }