import React, { useState } from 'react'; import { Button } from './ui/button'; import { Input } from './ui/input'; import { Label } from './ui/label'; import { Card } from './ui/card'; import { toast } from 'sonner'; import clareAvatar from '../assets/dfe44dab3ad8cd93953eac4a3e68bd1a5f999653.png'; import type { User } from '../App'; interface LoginScreenProps { onLogin: (user: User) => void; } export function LoginScreen({ onLogin }: LoginScreenProps) { const [showForm, setShowForm] = useState(false); const [name, setName] = useState(''); const [emailOrId, setEmailOrId] = useState(''); const [submitting, setSubmitting] = useState(false); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); const n = name.trim(); const uid = emailOrId.trim(); if (!n || !uid) return; setSubmitting(true); try { // HF Space: same-origin call is correct (your FastAPI serves the SPA) const resp = await fetch('/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: n, user_id: uid }), }); const data = await resp.json().catch(() => ({})); if (!resp.ok || !data?.ok) { const msg = data?.error || `Login failed (HTTP ${resp.status})`; throw new Error(msg); } // Keep your existing App flow: user = { name, email } onLogin({ name: data.user?.name ?? n, email: data.user?.user_id ?? uid }); toast.success('Signed in'); } catch (err: any) { toast.error(err?.message || 'Login failed'); } finally { setSubmitting(false); } }; return (
Your AI teaching assistant for personalized learning