mirror of
https://github.com/arc53/DocsGPT.git
synced 2025-11-30 00:53:14 +00:00
(feat:oauth/drive) raw fe integrate
This commit is contained in:
117
frontend/public/google-drive-callback.html
Normal file
117
frontend/public/google-drive-callback.html
Normal file
@@ -0,0 +1,117 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Google Drive Authentication</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 100vh;
|
||||
margin: 0;
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
.container {
|
||||
text-align: center;
|
||||
background: white;
|
||||
padding: 2rem;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
||||
}
|
||||
.success {
|
||||
color: #4CAF50;
|
||||
}
|
||||
.error {
|
||||
color: #f44336;
|
||||
}
|
||||
.loading {
|
||||
color: #2196F3;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h2>Google Drive Authentication</h2>
|
||||
<div id="status" class="loading">Processing authentication...</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function getUrlParams() {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
return {
|
||||
code: urlParams.get('code'),
|
||||
error: urlParams.get('error'),
|
||||
state: urlParams.get('state')
|
||||
};
|
||||
}
|
||||
|
||||
async function handleCallback() {
|
||||
const params = getUrlParams();
|
||||
const statusDiv = document.getElementById('status');
|
||||
|
||||
if (params.error) {
|
||||
statusDiv.className = 'error';
|
||||
statusDiv.innerHTML = `Authentication failed: ${params.error}<br><br>
|
||||
<small>Please try again and make sure to:<br>
|
||||
1. Grant all requested permissions<br>
|
||||
2. Allow offline access when prompted<br>
|
||||
3. Complete the authorization process</small>`;
|
||||
setTimeout(() => window.close(), 5000);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!params.code) {
|
||||
statusDiv.className = 'error';
|
||||
statusDiv.innerHTML = `No authorization code received.<br><br>
|
||||
<small>Please try again and make sure to complete the authorization process.</small>`;
|
||||
setTimeout(() => window.close(), 5000);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Exchange code for tokens
|
||||
// Use the backend API URL directly since this is a static HTML file
|
||||
const backendApiUrl = window.location.protocol + '//' + window.location.hostname + ':7091';
|
||||
const response = await fetch(backendApiUrl + '/api/google-drive/callback?' + window.location.search.substring(1));
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
// Store session token instead of token_info
|
||||
if (data.session_token) {
|
||||
localStorage.setItem('google_drive_session_token', data.session_token);
|
||||
}
|
||||
|
||||
// Extract user email
|
||||
let userEmail = data.user_email || 'Connected User';
|
||||
|
||||
statusDiv.className = 'success';
|
||||
statusDiv.innerHTML = `Authentication successful as ${userEmail}!<br><br>
|
||||
<small>You can close this window. Your Google Drive is now connected and ready to use.</small>`;
|
||||
|
||||
// Notify parent window with session token instead of token_info
|
||||
if (window.opener) {
|
||||
window.opener.postMessage({
|
||||
type: 'google_drive_auth_success',
|
||||
session_token: data.session_token,
|
||||
user_email: userEmail
|
||||
}, '*');
|
||||
}
|
||||
|
||||
setTimeout(() => window.close(), 3000);
|
||||
} else {
|
||||
throw new Error(data.error || 'Authentication failed');
|
||||
}
|
||||
} catch (error) {
|
||||
statusDiv.className = 'error';
|
||||
statusDiv.innerHTML = `Error: ${error.message}<br><br>
|
||||
<small>If this is an authentication error, please try again and make sure to grant offline access. You may need to revoke previous access to this app in your Google Account settings and re-authorize.</small>`;
|
||||
setTimeout(() => window.close(), 5000);
|
||||
}
|
||||
}
|
||||
|
||||
// Run when page loads
|
||||
handleCallback();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user