@@ -23,33 +23,54 @@ export function setupOtpInputs() {
2323}
2424
2525// Request OTP from server (using GET)
26+ // Request OTP from server (using JSONP workaround)
2627export async function requestOtp ( ) {
27- const email = DOM . loginEmail . value . trim ( ) ;
28-
29- if ( ! email || ! / ^ \S + @ \S + \. \S + $ / . test ( email ) ) {
30- await showAlert ( 'error' , 'Invalid Email' , 'Please enter a valid email address' ) ;
31- return false ;
32- }
28+ const email = DOM . loginEmail . value . trim ( ) ;
29+
30+ if ( ! email || ! / ^ \S + @ \S + \. \S + $ / . test ( email ) ) {
31+ await showAlert ( 'error' , 'Invalid Email' , 'Please enter a valid email address' ) ;
32+ return false ;
33+ }
3334
34- try {
35- const url = `${ CONFIG . googleScriptUrl } ?action=requestOtp&email=${ encodeURIComponent ( email ) } ` ;
36- const response = await fetch ( url ) ;
37- const result = await response . json ( ) ;
38-
39- if ( result . status === 'success' ) {
40- DOM . emailForm . classList . add ( 'hidden' ) ;
41- DOM . otpForm . classList . remove ( 'hidden' ) ;
42- DOM . otpEmailDisplay . textContent = email ;
43- return true ;
44- } else {
45- await showAlert ( 'error' , 'Error' , result . message || 'Failed to send OTP' ) ;
46- return false ;
47- }
48- } catch ( error ) {
49- console . error ( 'OTP request error:' , error ) ;
50- await showAlert ( 'error' , 'Network Error' , 'Failed to connect to server' ) ;
51- return false ;
52- }
35+ try {
36+ // Use JSONP approach
37+ const callbackName = `jsonp_${ Date . now ( ) } ` ;
38+ const url = `${ CONFIG . googleScriptUrl } ?action=requestOtp&email=${ encodeURIComponent ( email ) } &callback=${ callbackName } ` ;
39+
40+ return new Promise ( ( resolve ) => {
41+ window [ callbackName ] = ( response ) => {
42+ delete window [ callbackName ] ;
43+
44+ if ( response . status === 'success' ) {
45+ DOM . emailForm . classList . add ( 'hidden' ) ;
46+ DOM . otpForm . classList . remove ( 'hidden' ) ;
47+ DOM . otpEmailDisplay . textContent = email ;
48+ resolve ( true ) ;
49+ } else {
50+ showAlert ( 'error' , 'Error' , response . message || 'Failed to send OTP' ) ;
51+ resolve ( false ) ;
52+ }
53+ } ;
54+
55+ // Create script tag to make JSONP request
56+ const script = document . createElement ( 'script' ) ;
57+ script . src = url ;
58+ document . body . appendChild ( script ) ;
59+
60+ // Fallback timeout
61+ setTimeout ( ( ) => {
62+ if ( window [ callbackName ] ) {
63+ delete window [ callbackName ] ;
64+ showAlert ( 'error' , 'Timeout' , 'Server response timed out' ) ;
65+ resolve ( false ) ;
66+ }
67+ } , 10000 ) ;
68+ } ) ;
69+ } catch ( error ) {
70+ console . error ( 'OTP request error:' , error ) ;
71+ await showAlert ( 'error' , 'Network Error' , 'Failed to connect to server' ) ;
72+ return false ;
73+ }
5374}
5475
5576// Verify OTP with server (using GET)
0 commit comments