import { useState } from "react"; import { GOOGLE_AUTH_URL, AUTH_STATUS_URL } from "../lib/config"; type AuthMode = 'google' | 'csv-only'; interface AuthStatusProps { teacherEmail: string; setTeacherEmail: (email: string) => void; isAuthenticated: boolean; setIsAuthenticated: (auth: boolean) => void; } export function AuthStatus({ teacherEmail, setTeacherEmail, isAuthenticated, setIsAuthenticated, }: AuthStatusProps) { const [emailInput, setEmailInput] = useState(""); const [isChecking, setIsChecking] = useState(false); const [error, setError] = useState(null); const [authMode, setAuthMode] = useState(null); const checkAuthStatus = async (email: string) => { try { setIsChecking(true); const response = await fetch(`${AUTH_STATUS_URL}?teacher_email=${encodeURIComponent(email)}`); const data = await response.json(); if (data.authenticated) { setTeacherEmail(email); setIsAuthenticated(true); } return data.authenticated; } catch (err) { console.error("Error checking auth status:", err); return false; } finally { setIsChecking(false); } }; const handleConnect = async (e: React.FormEvent) => { e.preventDefault(); setError(null); if (!emailInput.trim()) { setError("請輸入您的電子郵件地址"); return; } // Check if already authenticated const alreadyAuth = await checkAuthStatus(emailInput); if (alreadyAuth) { return; } // Try to start OAuth - catch errors if not configured try { const response = await fetch(`${GOOGLE_AUTH_URL}?teacher_email=${encodeURIComponent(emailInput)}`, { method: 'GET', redirect: 'manual' }); // If we get a redirect, OAuth is configured - follow it if (response.type === 'opaqueredirect' || response.status === 302) { window.location.href = `${GOOGLE_AUTH_URL}?teacher_email=${encodeURIComponent(emailInput)}`; } else if (response.status === 500) { const data = await response.json(); if (data.detail?.includes('not configured')) { setAuthMode('csv-only'); setError("Google OAuth 未設定。您仍可在聊天中直接上傳 CSV 檔案使用應用程式!"); // Set as "connected" with just email for CSV fallback setTeacherEmail(emailInput); setIsAuthenticated(true); } else { setAuthMode('google'); setError(data.detail || "Failed to connect"); } } else { // Redirect to OAuth window.location.href = `${GOOGLE_AUTH_URL}?teacher_email=${encodeURIComponent(emailInput)}`; } } catch (err) { // Network error or CORS - just redirect window.location.href = `${GOOGLE_AUTH_URL}?teacher_email=${encodeURIComponent(emailInput)}`; } }; const handleDisconnect = async () => { try { await fetch(`/auth/disconnect?teacher_email=${encodeURIComponent(teacherEmail)}`, { method: 'POST' }); setIsAuthenticated(false); setTeacherEmail(""); } catch (err) { console.error("Error disconnecting:", err); } }; return (

{isAuthenticated ? "已連接!" : "連接您的 Google 帳號"}

{isAuthenticated ? "您的 Google 帳號已連結。現在可以分析考試答案了。" : "我們只會存取您的 Google 表單回應試算表。不會在我們的伺服器上儲存任何資料。"}

{isAuthenticated ? (
{teacherEmail}
{authMode === 'csv-only' ? 'CSV 上傳模式(Google OAuth 未設定)' : 'Google 帳號已連接'}
{authMode === 'csv-only' && (
注意:您可以在聊天中直接貼上 CSV 資料,或設定 Google OAuth 從 Google 表單取得資料。
)}
) : (
setEmailInput(e.target.value)} placeholder="teacher@school.edu" className="input" required />
{error && (
{error}
)}

連接即表示您同意我們的{" "} 服務條款 {" "}和{" "} 隱私政策

)}
); }