Implemented sync functionality with sheets and email sending

This commit is contained in:
Roman Krček
2025-07-08 15:30:37 +02:00
parent 39bd172798
commit 5bd642b947
10 changed files with 1208 additions and 65 deletions

View File

@@ -24,6 +24,19 @@ export const GET: RequestHandler = async ({ url }) => {
throw redirect(302, '/private/events?error=incomplete_tokens');
}
// Get user info to retrieve email
let userEmail = '';
try {
oauth.setCredentials(tokens);
const { google } = await import('googleapis');
const oauth2 = google.oauth2({ version: 'v2', auth: oauth });
const userInfo = await oauth2.userinfo.get();
userEmail = userInfo.data.email ?? '';
} catch (emailError) {
console.error('Error fetching user email:', emailError);
// Continue without email - it's not critical for the auth flow
}
// Create a success page with tokens that closes the popup and communicates with parent
const html = `
<!DOCTYPE html>
@@ -67,6 +80,7 @@ export const GET: RequestHandler = async ({ url }) => {
// Store tokens in localStorage (same origin)
localStorage.setItem('google_access_token', '${tokens.access_token}');
localStorage.setItem('google_refresh_token', '${tokens.refresh_token}');
${userEmail ? `localStorage.setItem('google_user_email', '${userEmail}');` : ''}
// Set timestamp that the main application will detect
localStorage.setItem('google_auth_timestamp', Date.now().toString());