/* ============================================================
login.jsx — production sign-in + self-serve signup (mail-free).
One universal login: email + password or Google. The account's role
decides the app after sign-in (no persona picker). New companies
self-register (company + GSTIN + owner) and are signed straight in as
the finance owner. Demo logins are tucked into a dev-only panel.
============================================================ */
// "Sign in with Google" — renders via Google Identity Services once its script
// (loaded async in index.html) is ready; hands back a signed ID token.
function GoogleButton({ clientId, onCredential }) {
const ref = React.useRef(null);
const [busy, setBusy] = React.useState(false);
const [err, setErr] = React.useState("");
const desktop = !!(window.isDesktopApp && window.isDesktopApp());
React.useEffect(() => {
// WEB only: render Google's GIS button. Desktop uses the native flow below (GIS can't
// run in the webview), so the effect no-ops there.
if (desktop || !clientId || !ref.current) return;
let tries = 0;
const t = setInterval(() => {
const g = window.google && window.google.accounts && window.google.accounts.id;
if (g) {
clearInterval(t);
window.google.accounts.id.initialize({
client_id: clientId,
callback: (resp) => onCredential(resp.credential),
});
window.google.accounts.id.renderButton(ref.current, {
theme: "outline", size: "large", width: 300, text: "continue_with",
});
} else if (++tries > 60) {
clearInterval(t);
}
}, 100);
return () => clearInterval(t);
}, [clientId, desktop]);
if (desktop) {
// DESKTOP: open the system browser (native loopback + PKCE) → id_token → login
return (