Spaces:
Sleeping
Sleeping
File size: 6,274 Bytes
f871fed |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
'use client'
import { useState, useEffect } from 'react'
import { useRouter } from 'next/navigation'
import { useAuth } from '@/lib/hooks/use-auth'
import { useAuthStore } from '@/lib/stores/auth-store'
import { getConfig } from '@/lib/config'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { AlertCircle } from 'lucide-react'
import { LoadingSpinner } from '@/components/common/LoadingSpinner'
export function LoginForm() {
const [password, setPassword] = useState('')
const { login, isLoading, error } = useAuth()
const { authRequired, checkAuthRequired, hasHydrated, isAuthenticated } = useAuthStore()
const [isCheckingAuth, setIsCheckingAuth] = useState(true)
const [configInfo, setConfigInfo] = useState<{ apiUrl: string; version: string; buildTime: string } | null>(null)
const router = useRouter()
// Load config info for debugging
useEffect(() => {
getConfig().then(cfg => {
setConfigInfo({
apiUrl: cfg.apiUrl,
version: cfg.version,
buildTime: cfg.buildTime,
})
}).catch(err => {
console.error('Failed to load config:', err)
})
}, [])
// Check if authentication is required on mount
useEffect(() => {
if (!hasHydrated) {
return
}
const checkAuth = async () => {
try {
const required = await checkAuthRequired()
// If auth is not required, redirect to notebooks
if (!required) {
router.push('/notebooks')
}
} catch (error) {
console.error('Error checking auth requirement:', error)
// On error, assume auth is required to be safe
} finally {
setIsCheckingAuth(false)
}
}
// If we already know auth status, use it
if (authRequired !== null) {
if (!authRequired && isAuthenticated) {
router.push('/notebooks')
} else {
setIsCheckingAuth(false)
}
} else {
void checkAuth()
}
}, [hasHydrated, authRequired, checkAuthRequired, router, isAuthenticated])
// Show loading while checking if auth is required
if (!hasHydrated || isCheckingAuth) {
return (
<div className="min-h-screen flex items-center justify-center bg-background">
<LoadingSpinner />
</div>
)
}
// If we still don't know if auth is required (connection error), show error
if (authRequired === null) {
return (
<div className="min-h-screen flex items-center justify-center bg-background p-4">
<Card className="w-full max-w-md">
<CardHeader className="text-center">
<CardTitle>Connection Error</CardTitle>
<CardDescription>
Unable to connect to the API server
</CardDescription>
</CardHeader>
<CardContent>
<div className="space-y-4">
<div className="flex items-start gap-2 text-red-600 text-sm">
<AlertCircle className="h-4 w-4 mt-0.5 flex-shrink-0" />
<div className="flex-1">
{error || 'Unable to connect to server. Please check if the API is running.'}
</div>
</div>
{configInfo && (
<div className="space-y-2 text-xs text-muted-foreground border-t pt-3">
<div className="font-medium">Diagnostic Information:</div>
<div className="space-y-1 font-mono">
<div>Version: {configInfo.version}</div>
<div>Built: {new Date(configInfo.buildTime).toLocaleString()}</div>
<div className="break-all">API URL: {configInfo.apiUrl}</div>
<div className="break-all">Frontend: {typeof window !== 'undefined' ? window.location.href : 'N/A'}</div>
</div>
<div className="text-xs pt-2">
Check browser console for detailed logs (look for 🔧 [Config] messages)
</div>
</div>
)}
<Button
onClick={() => window.location.reload()}
className="w-full"
>
Retry Connection
</Button>
</div>
</CardContent>
</Card>
</div>
)
}
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()
if (password.trim()) {
try {
await login(password)
} catch (error) {
console.error('Unhandled error during login:', error)
// The auth store should handle most errors, but this catches any unhandled ones
}
}
}
return (
<div className="min-h-screen flex items-center justify-center bg-background p-4">
<Card className="w-full max-w-md">
<CardHeader className="text-center">
<CardTitle>Open Notebook</CardTitle>
<CardDescription>
Enter your password to access the application
</CardDescription>
</CardHeader>
<CardContent>
<form onSubmit={handleSubmit} className="space-y-4">
<div>
<Input
type="password"
placeholder="Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
disabled={isLoading}
/>
</div>
{error && (
<div className="flex items-center gap-2 text-red-600 text-sm">
<AlertCircle className="h-4 w-4" />
{error}
</div>
)}
<Button
type="submit"
className="w-full"
disabled={isLoading || !password.trim()}
>
{isLoading ? 'Signing in...' : 'Sign In'}
</Button>
{configInfo && (
<div className="text-xs text-center text-muted-foreground pt-2 border-t">
<div>Version {configInfo.version}</div>
<div className="font-mono text-[10px]">{configInfo.apiUrl}</div>
</div>
)}
</form>
</CardContent>
</Card>
</div>
)
} |