import { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { authApi } from '../api/client'; import { useStore } from '../store/useStore'; import { Loader2, Wallet } from 'lucide-react'; import { FcGoogle } from 'react-icons/fc'; const isTauriEnv = () => typeof window !== 'undefined' && '__TAURI_INTERNALS__' in window; const DEEP_LINK_SCHEME = 'com.arrelin.family-budget-android://auth'; export default function Login() { const { t } = useTranslation(); const { setUser } = useStore(); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); useEffect(() => { if (!isTauriEnv()) return; let unlisten: (() => void) | null = null; const setupDeepLink = async () => { const { onOpenUrl } = await import('@tauri-apps/plugin-deep-link'); unlisten = await onOpenUrl(async (urls) => { const url = Array.isArray(urls) ? urls[0] : urls; if (!url.startsWith(DEEP_LINK_SCHEME)) return; const token = new URL(url).searchParams.get('token'); if (!token) return; try { setLoading(true); await authApi.mobileCallback(token); const me = await authApi.me(); setUser(me.data); } catch { setError(t('login.authError')); setLoading(false); } }); }; setupDeepLink(); return () => { unlisten?.(); }; }, []); const handleGoogleLogin = async () => { try { setLoading(true); setError(''); if (isTauriEnv()) { const { open } = await import('@tauri-apps/plugin-shell'); const response = await authApi.getGoogleAuthUrl(undefined, true); await open(response.data.url); return; } const currentUrl = window.location.origin; const response = await authApi.getGoogleAuthUrl(currentUrl); window.location.href = response.data.url; } catch (err) { setError(t('login.authError')); console.error(err); setLoading(false); } }; return (
{t('login.subtitle')}