import React, { useEffect, useMemo, useState } from "react"; import { Box, Button, Paper, Typography } from "@mui/material"; import { clearAuthToken, decodeJwtPayload, getAuthToken, setAuthToken } from "../auth"; import { useToast } from "../toast"; declare global { interface Window { google?: any; } } function loadGoogleScript(): Promise { return new Promise((resolve, reject) => { if (window.google?.accounts?.id) return resolve(); const existing = document.getElementById("google-gsi"); if (existing) { existing.addEventListener("load", () => resolve()); return; } const s = document.createElement("script"); s.id = "google-gsi"; s.src = "https://accounts.google.com/gsi/client"; s.async = true; s.defer = true; s.onload = () => resolve(); s.onerror = () => reject(new Error("Failed to load Google script")); document.head.appendChild(s); }); } export default function GoogleAuthCard({ onSignedIn }: { onSignedIn?: () => void }) { const { toast } = useToast(); const [token, setToken] = useState(() => getAuthToken()); const clientId = (process.env.REACT_APP_GOOGLE_CLIENT_ID || "").trim(); const payload = useMemo(() => (token ? decodeJwtPayload(token) : null), [token]); const email = payload?.email as string | undefined; useEffect(() => { if (!clientId) return; void loadGoogleScript() .then(() => { if (!window.google?.accounts?.id) return; window.google.accounts.id.initialize({ client_id: clientId, callback: (resp: any) => { if (resp?.credential) { setAuthToken(resp.credential); setToken(resp.credential); toast("Signed in.", "success"); onSignedIn?.(); } }, }); window.google.accounts.id.renderButton(document.getElementById("gsi-btn"), { theme: "outline", size: "large", type: "standard", shape: "pill", }); }) .catch(() => toast("Google auth script failed to load.", "error")); }, [clientId, onSignedIn, toast]); return ( Authentication (Google) {!clientId && ( Set `REACT_APP_GOOGLE_CLIENT_ID` in your UI environment to enable sign-in. )} {clientId && (
{token ? ( <> Signed in{email ? ` as ${email}` : ""}. ) : ( Sign in to unlock API access. )} )} ); }