Designed 🎨 & Developed By 👨🏻‍💻

Agnikul
let googleInitialized = false; let GOOGLE_CLIENT_ID = null; // Get Google Client ID from backend or environment async function getGoogleClientId() { try { const response = await fetch('/api/method/get_google_client_id'); const data = await response.json(); if (data.message && data.message.client_id) { GOOGLE_CLIENT_ID = data.message.client_id; return true; } } catch (error) { console.error('[v0] Error fetching Google Client ID:', error); } return false; } // Initialize Google Sign-In when the API loads async function initializeGoogleSignIn() { if (typeof google !== 'undefined' && google.accounts && google.accounts.id) { try { // Get client ID from backend if not already fetched if (!GOOGLE_CLIENT_ID) { const clientIdFetched = await getGoogleClientId(); if (!clientIdFetched) { console.error('[v0] Could not fetch Google Client ID'); showApiError('Google Sign-In configuration error. Please contact administrator.'); return; } } google.accounts.id.initialize({ client_id: GOOGLE_CLIENT_ID, callback: handleGoogleSignIn, auto_select: false, cancel_on_tap_outside: true }); googleInitialized = true; console.log('[v0] Google Sign-In initialized successfully'); // Hide any previous error messages hideApiError(); } catch (error) { console.error('[v0] Error initializing Google Sign-In:', error); showApiError('Failed to initialize Google Sign-In. Please refresh the page.'); } } else { console.log('[v0] Google API not ready, retrying...'); setTimeout(initializeGoogleSignIn, 100); } } function showApiError(message) { const errorDiv = document.getElementById('api-error-message'); if (errorDiv) { errorDiv.textContent = message; errorDiv.style.display = 'block'; } } function hideApiError() { const errorDiv = document.getElementById('api-error-message'); if (errorDiv) { errorDiv.style.display = 'none'; } } function showApiSuccess(message) { const successDiv = document.getElementById('api-success-message'); if (successDiv) { successDiv.textContent = message; successDiv.style.display = 'block'; } } function handleGoogleSignIn(response) { console.log('[v0] Google Sign-In response received'); if (response.credential) { // Show loading state const googleBtn = document.getElementById('google-signin-btn'); if (googleBtn) { googleBtn.disabled = true; googleBtn.innerHTML = 'Signing in...'; } // Send the credential to your Frappe backend fetch('/api/method/login_with_google', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Frappe-CSRF-Token': frappe.csrf_token || '' }, body: JSON.stringify({ credential: response.credential }) }) .then(response => response.json()) .then(data => { console.log('[v0] Backend response:', data); if (data.message && data.message.success) { showApiSuccess('Login successful! Redirecting...'); // Redirect to dashboard or home page setTimeout(() => { window.location.href = data.message.redirect_url || '/erp-desk'; }, 1000); } else { const errorMessage = data.message?.error || data.exc || 'Login failed. Please try again.'; showApiError(errorMessage); resetGoogleButton(); } }) .catch(error => { console.error('[v0] Error during login:', error); showApiError('Network error. Please check your connection and try again.'); resetGoogleButton(); }); } else { showApiError('Google Sign-In was cancelled or failed.'); resetGoogleButton(); } } function resetGoogleButton() { const googleBtn = document.getElementById('google-signin-btn'); if (googleBtn) { googleBtn.disabled = false; googleBtn.innerHTML = ` Sign in with Google `; } } // Initialize when page loads document.addEventListener('DOMContentLoaded', function() { setTimeout(async () => { await initializeGoogleSignIn(); }, 500); // Add click handler for Google Sign-In button const googleSignInBtn = document.getElementById('google-signin-btn'); if (googleSignInBtn) { googleSignInBtn.addEventListener('click', async function(e) { e.preventDefault(); console.log('[v0] Google Sign-In button clicked'); // Clear any previous error messages hideApiError(); if (!googleInitialized) { console.log('[v0] Google not initialized, attempting to initialize...'); showApiError('Initializing Google Sign-In...'); await initializeGoogleSignIn(); setTimeout(() => { if (googleInitialized) { hideApiError(); google.accounts.id.prompt(); } else { showApiError('Google Sign-In is not available. Please refresh the page and try again.'); } }, 1000); } else { try { google.accounts.id.prompt(); } catch (error) { console.error('[v0] Error showing Google prompt:', error); showApiError('Error launching Google Sign-In. Please try again.'); } } }); } }); // Removed all custom Google Sign-In JavaScript since we're now using Frappe's OAuth flow let emailTouched = false; let passwordTouched = false; let recaptchaCompleted = false; let forgotPasswordProcessing = false; // All Google Sign-In functionality now handled by Frappe's OAuth system