From ca3176072885f70c14727f0a837f94d619009760 Mon Sep 17 00:00:00 2001 From: syui Date: Thu, 19 Jun 2025 14:48:54 +0900 Subject: [PATCH] fix hugo callback --- my-blog/static/js/ask-ai.js | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/my-blog/static/js/ask-ai.js b/my-blog/static/js/ask-ai.js index ea23332..ab84092 100644 --- a/my-blog/static/js/ask-ai.js +++ b/my-blog/static/js/ask-ai.js @@ -284,8 +284,52 @@ function setupAskAIEventListeners() { }); } +// OAuth Callback handling +function handleOAuthCallback() { + // Check if we're on the callback page + if (window.location.pathname === '/oauth/callback') { + const urlParams = new URLSearchParams(window.location.search); + const code = urlParams.get('code'); + const error = urlParams.get('error'); + + if (error) { + console.error('OAuth error:', error); + // Redirect to home page with error + setTimeout(() => { + window.location.href = '/?oauth_error=' + encodeURIComponent(error); + }, 1000); + return; + } + + if (code) { + console.log('OAuth callback successful, code received'); + + // Get the original page from localStorage or use home page + const originalPage = localStorage.getItem('oauth_original_page') || '/'; + localStorage.removeItem('oauth_original_page'); + + // Wait a bit for OAuth app to process the callback + setTimeout(() => { + console.log('Redirecting back to:', originalPage); + window.location.href = originalPage; + }, 2000); + + return; + } + } + + // Store current page before OAuth if we're not on callback page + if (window.location.pathname !== '/oauth/callback') { + localStorage.setItem('oauth_original_page', window.location.href); + } +} + // Initialize Ask AI when DOM is loaded document.addEventListener('DOMContentLoaded', function() { + // Handle OAuth callback first + handleOAuthCallback(); + + // Then initialize Ask AI setupAskAIEventListeners(); console.log('Ask AI initialized successfully'); });