Fixed google login
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { isTokenValid, getUserInfo, revokeToken } from '$lib/google/auth/client.js';
|
||||
import type { GoogleSheet } from '$lib/google/sheets/client.js';
|
||||
import type { GoogleSheet } from '$lib/google/sheets/types.ts';
|
||||
import { goto } from '$app/navigation';
|
||||
|
||||
// Import Components
|
||||
@@ -124,35 +124,49 @@
|
||||
let popupTimer: number | null = null;
|
||||
let cancelTimeout: number | null = null;
|
||||
|
||||
// Listen for messages from the popup
|
||||
const messageHandler = (event: MessageEvent) => {
|
||||
if (event.data?.type === 'GOOGLE_AUTH_SUCCESS') {
|
||||
authCompleted = true;
|
||||
authData.connecting = false;
|
||||
authData.showCancelOption = false;
|
||||
window.removeEventListener('message', messageHandler);
|
||||
// Store current timestamp to detect changes in localStorage
|
||||
const startTimestamp = localStorage.getItem('google_auth_timestamp') || '0';
|
||||
|
||||
// Poll localStorage for auth completion
|
||||
const pollInterval = setInterval(() => {
|
||||
try {
|
||||
const currentTimestamp = localStorage.getItem('google_auth_timestamp');
|
||||
|
||||
// Clean up timers
|
||||
if (popupTimer) clearTimeout(popupTimer);
|
||||
if (cancelTimeout) clearTimeout(cancelTimeout);
|
||||
|
||||
// Check auth status again after success
|
||||
setTimeout(checkGoogleAuth, 100);
|
||||
// If timestamp has changed, auth is complete
|
||||
if (currentTimestamp && currentTimestamp !== startTimestamp) {
|
||||
handleAuthSuccess();
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error checking auth timestamp:', e);
|
||||
}
|
||||
};
|
||||
|
||||
}, 500); // Poll every 500ms
|
||||
|
||||
// Common handler for authentication success
|
||||
function handleAuthSuccess() {
|
||||
if (authCompleted) return; // Prevent duplicate handling
|
||||
|
||||
authCompleted = true;
|
||||
authData.connecting = false;
|
||||
authData.showCancelOption = false;
|
||||
|
||||
// Clean up timers
|
||||
clearInterval(pollInterval);
|
||||
if (popupTimer) clearTimeout(popupTimer);
|
||||
if (cancelTimeout) clearTimeout(cancelTimeout);
|
||||
|
||||
// Update auth state
|
||||
setTimeout(checkGoogleAuth, 100);
|
||||
}
|
||||
|
||||
// Clean up function to handle all cleanup in one place
|
||||
const cleanUp = () => {
|
||||
window.removeEventListener('message', messageHandler);
|
||||
clearInterval(pollInterval);
|
||||
if (popupTimer) clearTimeout(popupTimer);
|
||||
if (cancelTimeout) clearTimeout(cancelTimeout);
|
||||
authData.connecting = false;
|
||||
};
|
||||
|
||||
window.addEventListener('message', messageHandler);
|
||||
|
||||
// Set a timeout to check auth status regardless of popup state
|
||||
// This is a workaround for Cross-Origin-Opener-Policy restrictions
|
||||
// Set a timeout for initial auth check
|
||||
popupTimer = setTimeout(() => {
|
||||
// Only check if auth isn't already completed
|
||||
if (!authCompleted) {
|
||||
@@ -160,21 +174,21 @@
|
||||
// Check if tokens were stored by the popup before it was closed
|
||||
setTimeout(checkGoogleAuth, 100);
|
||||
}
|
||||
}, 60 * 1000) as unknown as number;
|
||||
}, 30 * 1000) as unknown as number; // Reduced from 60s to 30s
|
||||
|
||||
// After 20 seconds with no response, show cancel option
|
||||
// Show cancel option sooner
|
||||
cancelTimeout = setTimeout(() => {
|
||||
if (!authCompleted) {
|
||||
authData.showCancelOption = true;
|
||||
}
|
||||
}, 20 * 1000) as unknown as number;
|
||||
}, 10 * 1000) as unknown as number; // Reduced from 20s to 10s
|
||||
|
||||
// Set a final timeout to clean up everything if nothing else worked
|
||||
// Final cleanup timeout
|
||||
setTimeout(() => {
|
||||
if (!authCompleted) {
|
||||
cleanUp();
|
||||
}
|
||||
}, 3 * 60 * 1000); // 3 minute max timeout
|
||||
}, 60 * 1000); // Reduced from 3min to 1min
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error connecting to Google:', error);
|
||||
|
||||
Reference in New Issue
Block a user