@@ -33,65 +33,63 @@ <h2>OAuth Authorization</h2>
3333 </ div >
3434
3535 < script >
36- ( function ( ) {
37- const statusDiv = document . getElementById ( 'status' ) ;
38-
39- try {
40- // Parse URL fragment for access_token
41- const hash = window . location . hash . substring ( 1 ) ;
42- const params = new URLSearchParams ( hash ) ;
43-
44- const accessToken = params . get ( 'access_token ' ) ;
45- const error = params . get ( 'error' ) ;
46-
47- if ( accessToken ) {
48- statusDiv . innerHTML = '<div class="success">✓ Authorization successful!<br>You can close this window.</div>' ;
49-
50- // Send token back to parent window
51- if ( window . opener ) {
36+ ( function ( ) {
37+ const statusDiv = document . getElementById ( 'status' ) ;
38+ try {
39+ // Parse URL fragment for access_token
40+ const hash = window . location . hash . startsWith ( '#' ) ? window . location . hash . substring ( 1 ) : window . location . hash ;
41+ const params = new URLSearchParams ( hash ) ;
42+
43+ const accessToken = params . get ( 'access_token' ) ;
44+ const error = params . get ( 'error ' ) ;
45+
46+ if ( accessToken ) {
47+ statusDiv . innerHTML = '<div class="success">✓ Authorization successful!<br>Stänger fönstret...</div>' ;
48+
49+ // Send token back to parent window
50+ if ( window . opener ) {
51+ try {
5252 window . opener . postMessage ( {
5353 access_token : accessToken ,
5454 expires_in : params . get ( 'expires_in' ) ,
5555 token_type : params . get ( 'token_type' )
5656 } , '*' ) ;
57- }
58-
59- // Close after a brief delay
60- setTimeout ( ( ) => {
61- window . close ( ) ;
62- } , 2000 ) ;
63-
64- } else if ( error ) {
65- statusDiv . innerHTML = '<div class="error">✗ Authorization failed:<br>' + error + '</div>' ;
66-
67- if ( window . opener ) {
57+ } catch ( _ ) { }
58+ }
59+
60+ // Close after a short delay so the user sees the success briefly
61+ setTimeout ( ( ) => {
62+ try { window . close ( ) ; } catch ( _ ) { }
63+ } , 400 ) ;
64+
65+ } else if ( error ) {
66+ statusDiv . innerHTML = '<div class="error">✗ Authorization failed:<br>' + error + '</div>' ;
67+
68+ if ( window . opener ) {
69+ try {
6870 window . opener . postMessage ( {
6971 error : error ,
7072 error_description : params . get ( 'error_description' )
7173 } , '*' ) ;
72- }
73-
74- } else {
75- statusDiv . innerHTML = '<div class="error">✗ No authorization data received</div>' ;
76-
77- if ( window . opener ) {
78- window . opener . postMessage ( {
79- error : 'no_token_received'
80- } , '*' ) ;
81- }
74+ } catch ( _ ) { }
8275 }
83-
84- } catch ( e ) {
85- statusDiv . innerHTML = '<div class="error">✗ Error processing authorization:<br>' + e . message + ' </div>';
86-
76+
77+ } else {
78+ statusDiv . innerHTML = '<div class="error">✗ No authorization data received </div>' ;
79+
8780 if ( window . opener ) {
88- window . opener . postMessage ( {
89- error : 'processing_error' ,
90- error_description : e . message
91- } , '*' ) ;
81+ try { window . opener . postMessage ( { error : 'no_token_received' } , '*' ) ; } catch ( _ ) { }
9282 }
9383 }
94- } ) ( ) ;
84+
85+ } catch ( e ) {
86+ statusDiv . innerHTML = '<div class="error">✗ Error processing authorization:<br>' + e . message + '</div>' ;
87+
88+ if ( window . opener ) {
89+ try { window . opener . postMessage ( { error : 'processing_error' , error_description : e . message } , '*' ) ; } catch ( _ ) { }
90+ }
91+ }
92+ } ) ( ) ;
9593 </ script >
9694</ body >
9795</ html >
0 commit comments