Fixed google login
This commit is contained in:
@@ -26,7 +26,6 @@ export function getOAuthClient() {
|
||||
* @returns Auth URL for Google OAuth
|
||||
*/
|
||||
export function createAuthUrl() {
|
||||
console.warn("CREATE AUTH URL");
|
||||
return getOAuthClient().generateAuthUrl({
|
||||
access_type: 'offline',
|
||||
prompt: 'consent',
|
||||
|
||||
@@ -64,34 +64,29 @@ export const GET: RequestHandler = async ({ url }) => {
|
||||
<script>
|
||||
(function() {
|
||||
try {
|
||||
// Store tokens in the parent window's localStorage
|
||||
if (window.opener && !window.opener.closed) {
|
||||
window.opener.localStorage.setItem('google_access_token', '${tokens.access_token}');
|
||||
window.opener.localStorage.setItem('google_refresh_token', '${tokens.refresh_token}');
|
||||
|
||||
// Send success message to parent
|
||||
window.opener.postMessage({
|
||||
type: 'GOOGLE_AUTH_SUCCESS',
|
||||
tokens: {
|
||||
accessToken: '${tokens.access_token}',
|
||||
refreshToken: '${tokens.refresh_token}'
|
||||
}
|
||||
}, '*');
|
||||
|
||||
// Close the popup after a short delay to ensure message is received
|
||||
setTimeout(() => {
|
||||
// Store tokens in localStorage (same origin)
|
||||
localStorage.setItem('google_access_token', '${tokens.access_token}');
|
||||
localStorage.setItem('google_refresh_token', '${tokens.refresh_token}');
|
||||
// Set timestamp that the main application will detect
|
||||
localStorage.setItem('google_auth_timestamp', Date.now().toString());
|
||||
|
||||
// Update UI to show success
|
||||
document.querySelector('.loading').textContent = 'Authentication complete! This window will close automatically.';
|
||||
|
||||
// Close window after a short delay
|
||||
setTimeout(() => {
|
||||
try {
|
||||
window.close();
|
||||
}, 500);
|
||||
} else {
|
||||
// If no opener, close immediately
|
||||
window.close();
|
||||
}
|
||||
} catch (e) {
|
||||
// If we can't close automatically, update message
|
||||
document.querySelector('.loading').textContent = 'Authentication complete! You can close this window now.';
|
||||
}
|
||||
}, 1500);
|
||||
} catch (error) {
|
||||
console.error('Error in auth callback:', error);
|
||||
// Try to close the window anyway
|
||||
setTimeout(() => {
|
||||
window.close();
|
||||
}, 1000);
|
||||
// Update UI to show error
|
||||
document.querySelector('.success').textContent = '✗ Authentication error';
|
||||
document.querySelector('.loading').textContent = 'Please close this window and try again.';
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -36,6 +36,12 @@ self.addEventListener('fetch', (event) => {
|
||||
|
||||
async function respond() {
|
||||
const url = new URL(event.request.url);
|
||||
|
||||
// Skip caching for auth routes
|
||||
if (url.pathname.startsWith('/auth/')) {
|
||||
return fetch(event.request);
|
||||
}
|
||||
|
||||
const cache = await caches.open(CACHE);
|
||||
|
||||
// `build`/`files` can always be served from the cache
|
||||
|
||||
Reference in New Issue
Block a user