Skip to content

Commit cc26704

Browse files
committed
Reject promise in signIn is user closes auth window
1 parent 5a7ba5d commit cc26704

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/puter-js/src/modules/Auth.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,27 @@ class Auth{
5050
let title = 'Puter';
5151
var left = (screen.width/2)-(w/2);
5252
var top = (screen.height/2)-(h/2);
53-
window.open(puter.defaultGUIOrigin + '/action/sign-in?embedded_in_popup=true&msg_id=' + msg_id + (window.crossOriginIsolated ? '&cross_origin_isolated=true' : ''),
53+
54+
// Store reference to the popup window
55+
const popup = window.open(puter.defaultGUIOrigin + '/action/sign-in?embedded_in_popup=true&msg_id=' + msg_id + (window.crossOriginIsolated ? '&cross_origin_isolated=true' : ''),
5456
title,
5557
'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
5658

57-
window.addEventListener('message', function(e){
59+
// Set up interval to check if popup was closed
60+
const checkClosed = setInterval(() => {
61+
if (popup.closed) {
62+
clearInterval(checkClosed);
63+
// Remove the message listener
64+
window.removeEventListener('message', messageHandler);
65+
reject({ error: 'auth_window_closed', msg: 'Authentication window was closed by the user without completing the process.' });
66+
}
67+
}, 100);
68+
69+
function messageHandler(e) {
5870
if(e.data.msg_id == msg_id){
71+
// Clear the interval since we got a response
72+
clearInterval(checkClosed);
73+
5974
// remove redundant attributes
6075
delete e.data.msg_id;
6176
delete e.data.msg;
@@ -69,9 +84,11 @@ class Auth{
6984
reject(e.data);
7085

7186
// delete the listener
72-
window.removeEventListener('message', this);
87+
window.removeEventListener('message', messageHandler);
7388
}
74-
});
89+
}
90+
91+
window.addEventListener('message', messageHandler);
7592
});
7693
}
7794

0 commit comments

Comments
 (0)