feat: refactor OneDrive integration to use session storage for credentials and enhance error handling

feat: updated System Status Dashboard
This commit is contained in:
Christian Krakau-Louis
2025-04-03 12:44:17 +02:00
parent 94889e9e41
commit af1a76a813
6 changed files with 701 additions and 137 deletions
+35 -13
View File
@@ -92,24 +92,34 @@
<script>
document.addEventListener('DOMContentLoaded', function() {
const code = "{{ code }}";
const clientId = "{{ client_id_value }}";
// Get credentials from session storage (these take precedence over server-provided values)
const clientId = sessionStorage.getItem('onedrive_client_id') || "{{ client_id_value }}";
const clientSecret = sessionStorage.getItem('onedrive_client_secret') || "{{ client_secret_value }}";
const tenantId = sessionStorage.getItem('onedrive_tenant_id') || "{{ tenant_id }}" || "common";
const folderPath = sessionStorage.getItem('onedrive_folder_path') || "";
const redirectUri = window.location.origin + "/onedrive-callback";
const tenantId = "{{ tenant_id }}";
// Automatically exchange the code for a refresh token
if (code) {
exchangeCode(code, clientId, redirectUri, tenantId);
if (!clientId || !clientSecret) {
showError("Missing Client ID or Client Secret. Please go back to the setup page and try again.");
return;
}
exchangeCode(code, clientId, clientSecret, redirectUri, tenantId, folderPath);
} else {
showError("No authorization code was found in the URL");
}
function exchangeCode(code, clientId, redirectUri, tenantId) {
function exchangeCode(code, clientId, clientSecret, redirectUri, tenantId, folderPath) {
const formData = new FormData();
formData.append('client_id', clientId);
formData.append('client_secret', "{{ client_secret_value }}"); // Use the pre-configured secret
formData.append('client_secret', clientSecret);
formData.append('redirect_uri', redirectUri);
formData.append('code', code);
formData.append('tenant_id', tenantId || 'common');
formData.append('tenant_id', tenantId);
// Show more details in processing message
document.getElementById('processing-message').innerHTML =
@@ -134,9 +144,14 @@ document.addEventListener('DOMContentLoaded', function() {
const updateFormData = new FormData();
updateFormData.append('refresh_token', data.refresh_token);
// Use the existing clientId and tenantId if they were provided
if (clientId) updateFormData.append('client_id', clientId);
if (tenantId) updateFormData.append('tenant_id', tenantId);
// Use the values from session storage
updateFormData.append('client_id', clientId);
updateFormData.append('client_secret', clientSecret);
updateFormData.append('tenant_id', tenantId);
if (folderPath) {
updateFormData.append('folder_path', folderPath);
}
document.getElementById('processing-message').innerHTML =
'<p>Updating system settings with new token...</p>';
@@ -153,11 +168,18 @@ document.addEventListener('DOMContentLoaded', function() {
return response.json();
}).then(() => {
// Show the success message and environment variables
showSuccess(data.refresh_token, clientId, tenantId);
showSuccess(data.refresh_token, clientId, clientSecret, tenantId, folderPath);
// In 10 seconds, redirect to status page (giving more time to copy)
setTimeout(() => {
window.location.href = '/status';
}, 10000);
// Clean up session storage
sessionStorage.removeItem('onedrive_client_id');
sessionStorage.removeItem('onedrive_client_secret');
sessionStorage.removeItem('onedrive_tenant_id');
sessionStorage.removeItem('onedrive_folder_path');
});
} else {
throw new Error('No refresh token was received from the server');
@@ -174,7 +196,7 @@ document.addEventListener('DOMContentLoaded', function() {
document.getElementById('error-message').innerText = message;
}
function showSuccess(refreshToken, clientId, tenantId) {
function showSuccess(refreshToken, clientId, clientSecret, tenantId, folderPath) {
document.getElementById('processing-message').classList.add('hidden');
document.getElementById('success-container').classList.remove('hidden');
@@ -182,10 +204,10 @@ document.addEventListener('DOMContentLoaded', function() {
const envVarsCode = document.querySelector('#env-vars code');
if (envVarsCode) {
envVarsCode.textContent = `ONEDRIVE_CLIENT_ID=${clientId}
ONEDRIVE_CLIENT_SECRET={{ client_secret_value }}
ONEDRIVE_CLIENT_SECRET=${clientSecret}
ONEDRIVE_TENANT_ID=${tenantId || 'common'}
ONEDRIVE_REFRESH_TOKEN=${refreshToken}
ONEDRIVE_FOLDER_PATH=Documents/Uploads`;
ONEDRIVE_FOLDER_PATH=${folderPath || 'Documents/Uploads'}`;
}
// Add copy functionality