// Native auth using SocialLogin plugin + Firebase REST API // No Firebase SDK on native - avoids protocol errors // SocialLogin accessed via window.Capacitor.Plugins to avoid breaking web builds const FIREBASE_API_KEY = 'AIzaSyAVQWsCgnaIXOqtPLWOhb17Bk8C9R2hpOE'; function isNative() { return typeof window !== 'undefined' && window.Capacitor !== undefined; } function getSocialLogin() { if (isNative() && window.Capacitor.Plugins) { return window.Capacitor.Plugins.SocialLogin || null; } return null; } // Get stored user from localStorage export const getCurrentUser = () => { const stored = localStorage.getItem('dublfit_user'); return stored ? JSON.parse(stored) : null; }; // Sign in with Google export const signInWithGoogle = async () => { if (!isNative()) { throw new Error('Google sign-in only available in app'); } const SL = getSocialLogin(); if (!SL) { throw new Error('SocialLogin plugin not available'); } // Initialize and login with Google await SL.initialize({ google: { iOSClientId: '171636644437-k9fupe9qbjj1ru75gmjqjbjbq0am980u.apps.googleusercontent.com' } }); const res = await SL.login({ provider: 'google', options: { scopes: ['email', 'profile'] } }); console.log('SocialLogin response:', JSON.stringify(res, null, 2)); // Get the idToken - might be in different places depending on plugin version const idToken = res.result?.idToken || res.result?.authentication?.idToken || res.idToken; console.log('idToken:', idToken ? 'found' : 'NOT FOUND'); if (!idToken) { // Fallback: use Google profile directly without Firebase const profile = res.result?.profile || res.result; const user = { uid: profile.id, email: profile.email, displayName: profile.name, photoURL: profile.imageUrl }; console.log('Using Google profile directly (no idToken):', user); localStorage.setItem('dublfit_user', JSON.stringify(user)); return user; } // Exchange Google token for Firebase user via REST API console.log('Calling Firebase REST API...'); const response = await fetch( `https://identitytoolkit.googleapis.com/v1/accounts:signInWithIdp?key=${FIREBASE_API_KEY}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ postBody: `id_token=${idToken}&providerId=google.com`, requestUri: 'http://localhost', returnSecureToken: true }) } ); if (!response.ok) { const error = await response.json(); console.log('Firebase error:', error); throw new Error(error.error?.message || 'Firebase auth failed'); } const data = await response.json(); console.log('Firebase response - uid:', data.localId, 'email:', data.email); const user = { uid: data.localId, email: data.email, displayName: data.displayName, photoURL: data.photoUrl }; console.log('Logged in with Firebase uid:', user.uid); localStorage.setItem('dublfit_user', JSON.stringify(user)); return user; }; // Sign out export const signOut = async () => { localStorage.removeItem('dublfit_user'); // Also sign out of Google so next login shows account picker if (isNative()) { try { const SL = getSocialLogin(); if (SL) await SL.logout({ provider: 'google' }); } catch (e) { console.log('Google logout error (ok to ignore):', e); } } return Promise.resolve(); };