'use client' import { useRouter } from 'next/navigation' import { GoogleAuthProvider, signInWithPopup } from 'firebase/auth' import { auth } from '@/utils/firebase' import { Chrome } from 'lucide-react' import { useState, useEffect } from 'react' export default function LoginPage() { const router = useRouter() const [isLoading, setIsLoading] = useState(false) const [isElectronMode, setIsElectronMode] = useState(false) useEffect(() => { const urlParams = new URLSearchParams(window.location.search) const mode = urlParams.get('mode') setIsElectronMode(mode === 'electron') }, []) const handleGoogleSignIn = async () => { const provider = new GoogleAuthProvider() setIsLoading(true) try { const result = await signInWithPopup(auth, provider) const user = result.user if (user) { console.log('✅ Google login successful:', user.uid) if (isElectronMode) { try { const idToken = await user.getIdToken() const deepLinkUrl = `pickleglass://auth-success?` + new URLSearchParams({ uid: user.uid, email: user.email || '', displayName: user.displayName || '', token: idToken }).toString() console.log('🔗 Return to electron app via deep link:', deepLinkUrl) window.location.href = deepLinkUrl setTimeout(() => { alert('Login completed. Please return to Pickle Glass app.') }, 1000) } catch (error) { console.error('❌ Deep link processing failed:', error) alert('Login was successful but failed to return to app. Please check the app.') } } else if (typeof window !== 'undefined' && window.require) { try { const { ipcRenderer } = window.require('electron') const idToken = await user.getIdToken() ipcRenderer.send('firebase-auth-success', { uid: user.uid, displayName: user.displayName, email: user.email, idToken }) console.log('📡 Auth info sent to electron successfully') } catch (error) { console.error('❌ Electron communication failed:', error) } } else { router.push('/settings') } } } catch (error: any) { console.error('❌ Google login failed:', error) if (error.code !== 'auth/popup-closed-by-user') { alert('An error occurred during login. Please try again.') } } finally { setIsLoading(false) } } return (
Sign in with your Google account to sync your data across all devices.
{isElectronMode ? (🔗 Login requested from Electron app
) : (Local mode will run if you don't sign in.
)}By signing in, you agree to our Terms of Service and Privacy Policy.