Skip to content

Commit 238e617

Browse files
committed
[blerner-assisted] cleanup() cleanups
- Reorder removing a key with setting a handler to avoid a spurious event - Make sure cleanup clears localStorage and nulls out the channel - Add some useful debugging info we can look for in console.logs I tested this by commenting out cleanup and checking that all 3 modes triggered. Then I added +++ b/src/server.js @@ -45,6 +45,11 @@ function start(config, onServerReady) { app = express(); app.use(bodyParser.urlencoded({ extended: false })) app.use(bodyParser.json()) + app.use(function(req, res, next) { + res.setHeader("Cross-Origin-Opener-Policy", "noopener-allow-popups"); + next(); + }); + And verified that the postMessage failed, but the other two mechanisms succeed
1 parent 6e58236 commit 238e617

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

src/web/close.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// This is the fallback for environments like GoGuardian that inject COOP headers
1919
if (typeof BroadcastChannel !== 'undefined') {
2020
try {
21-
var channel = new BroadcastChannel('pyret_auth');
21+
let channel = new BroadcastChannel('pyret_auth');
2222
channel.postMessage({ type: 'auth_complete' });
2323
channel.close();
2424
} catch (e) {

src/web/js/google-apis/api-wrapper.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,38 @@ function reauth(immediate, useFullScopes) {
5454

5555
// Track whether we've already resolved to avoid double-resolution
5656
var resolved = false;
57-
function resolveOnce() {
57+
function resolveOnce(method) {
5858
if (!resolved) {
59+
console.log("INFO: Popup login resolved by: ", method);
5960
resolved = true;
61+
// NOTE(joe): A useful thing to do for testing is to comment out this
62+
// cleanup(), and check which of the 3 methods are returning success
63+
// here. cleanup() will stop others from triggering.
6064
cleanup();
6165
d.resolve(reauth(true, useFullScopes));
6266
}
67+
else {
68+
console.log("INFO: Popup login resolved again (ignored): ", method);
69+
}
6370
}
6471

6572
// Cleanup function to remove all listeners
6673
var channel = null;
6774
function cleanup() {
6875
window.removeEventListener('message', messageHandler);
6976
window.removeEventListener('storage', storageHandler);
77+
try { localStorage.removeItem('pyret_auth_complete'); } catch (err) {}
7078
if (channel) {
71-
try { channel.close(); } catch (e) {}
79+
try { channel.close(); }
80+
finally { channel = null; }
7281
}
7382
}
7483

7584
// Method 1: Traditional postMessage (works when COOP allows window.opener)
7685
function messageHandler(e) {
7786
// e.domain appears to not be defined in Firefox
7887
if ((e.domain || e.origin) === document.location.origin) {
79-
resolveOnce();
88+
resolveOnce("postMessage");
8089
}
8190
}
8291
window.addEventListener('message', messageHandler);
@@ -88,7 +97,7 @@ function reauth(immediate, useFullScopes) {
8897
channel = new BroadcastChannel('pyret_auth');
8998
channel.onmessage = function(e) {
9099
if (e.data && e.data.type === 'auth_complete') {
91-
resolveOnce();
100+
resolveOnce("Broadcast");
92101
}
93102
};
94103
} catch (e) {
@@ -99,14 +108,14 @@ function reauth(immediate, useFullScopes) {
99108
// Method 3: localStorage fallback for very old browsers without BroadcastChannel
100109
function storageHandler(e) {
101110
if (e.key === 'pyret_auth_complete') {
102-
resolveOnce();
111+
resolveOnce("localStorage");
103112
// Clean up the flag
104113
try { localStorage.removeItem('pyret_auth_complete'); } catch (err) {}
105114
}
106115
}
107-
window.addEventListener('storage', storageHandler);
108116
// Clear any stale auth flag before opening popup
109117
try { localStorage.removeItem('pyret_auth_complete'); } catch (e) {}
118+
window.addEventListener('storage', storageHandler);
110119

111120
// Need to do a login to get a cookie for this user; do it in a popup
112121
window.open(path);

0 commit comments

Comments
 (0)