Skip to content

Commit 16faa5f

Browse files
committed
refactor(auth): replace JSONP with fetch for OTP request and add fallback
Improve the OTP request logic by first attempting a regular fetch request, which is more modern and secure. If the fetch fails, fall back to the JSONP approach. This ensures better compatibility and error handling, including a timeout mechanism and proper cleanup of resources.
1 parent cc56795 commit 16faa5f

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

Assets/js/auth.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export function setupOtpInputs() {
2323
}
2424

2525
// Request OTP from server (using GET)
26-
// Request OTP from server (using JSONP workaround)
2726
export async function requestOtp() {
2827
const email = DOM.loginEmail.value.trim();
2928

@@ -33,13 +32,31 @@ export async function requestOtp() {
3332
}
3433

3534
try {
36-
// Use JSONP approach
37-
const callbackName = `jsonp_${Date.now()}`;
38-
const url = `${CONFIG.googleScriptUrl}?action=requestOtp&email=${encodeURIComponent(email)}&callback=${callbackName}`;
39-
35+
// First try regular fetch (works if CORS is properly configured)
36+
try {
37+
const url = `${CONFIG.googleScriptUrl}?action=requestOtp&email=${encodeURIComponent(email)}`;
38+
const response = await fetch(url);
39+
if (response.ok) {
40+
const result = await response.json();
41+
if (result.status === 'success') {
42+
DOM.emailForm.classList.add('hidden');
43+
DOM.otpForm.classList.remove('hidden');
44+
DOM.otpEmailDisplay.textContent = email;
45+
return true;
46+
}
47+
}
48+
} catch (fetchError) {
49+
console.log('Regular fetch failed, trying JSONP');
50+
}
51+
52+
// Fallback to JSONP if fetch fails
4053
return new Promise((resolve) => {
54+
const callbackName = `jsonp_${Date.now()}`;
55+
const url = `${CONFIG.googleScriptUrl}?action=requestOtp&email=${encodeURIComponent(email)}&callback=${callbackName}`;
56+
4157
window[callbackName] = (response) => {
4258
delete window[callbackName];
59+
document.body.removeChild(script);
4360

4461
if (response.status === 'success') {
4562
DOM.emailForm.classList.add('hidden');
@@ -52,15 +69,20 @@ export async function requestOtp() {
5269
}
5370
};
5471

55-
// Create script tag to make JSONP request
5672
const script = document.createElement('script');
5773
script.src = url;
74+
script.onerror = () => {
75+
delete window[callbackName];
76+
showAlert('error', 'Connection Error', 'Failed to connect to server');
77+
resolve(false);
78+
};
5879
document.body.appendChild(script);
5980

60-
// Fallback timeout
81+
// Timeout after 10 seconds
6182
setTimeout(() => {
6283
if (window[callbackName]) {
6384
delete window[callbackName];
85+
document.body.removeChild(script);
6486
showAlert('error', 'Timeout', 'Server response timed out');
6587
resolve(false);
6688
}

Assets/js/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Configuration constants
22
export const CONFIG = {
3-
googleScriptUrl: 'https://script.google.com/macros/s/AKfycbwnFQV7ukTBCUgGSjipMreyl4t-Zh3cxX4ARLepZ-k8-YL_qARV7chBXhMKVmVOhZ--/exec', // auth
3+
googleScriptUrl: 'https://script.google.com/macros/s/AKfycbx4YHIB02db77gwBh8bKWEtGgE8nJmZmVbD8278d6tYX_TvrXOclJMTy1yRGqqxVLNm/exec', // auth
44
sessionExpiryHours: 1, // Matches GAS session duration
55
otpExpiryMinutes: 5, // Matches GAS OTP duration
66

0 commit comments

Comments
 (0)