Skip to content

Commit 3a0d275

Browse files
committed
Handle OAuth flow more consistently so the user doesn't need to refresh the page
1 parent 80b6ffa commit 3a0d275

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/hf-exporter.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class HFDatasetExporter extends HTMLElement {
3737
};
3838

3939
// Listen for OAuth callback messages
40-
window.addEventListener('message', this.handleOAuthCallback.bind(this));
40+
this._boundHandleOAuthCallback = this.handleOAuthCallback.bind(this);
41+
window.addEventListener('message', this._boundHandleOAuthCallback);
4142

4243
// Add event listeners
4344
this.setupEventListeners();
@@ -51,7 +52,7 @@ class HFDatasetExporter extends HTMLElement {
5152
if (this.oauthPopup && !this.oauthPopup.closed) {
5253
this.oauthPopup.close();
5354
}
54-
window.removeEventListener('message', this.handleOAuthCallback.bind(this));
55+
window.removeEventListener('message', this._boundHandleOAuthCallback);
5556
}
5657

5758
setupEventListeners() {
@@ -237,6 +238,11 @@ class HFDatasetExporter extends HTMLElement {
237238
}
238239

239240
async handleOAuthCallback(event) {
241+
// Ignore postMessages that aren't OAuth callbacks (e.g. from Angular, analytics, iframes)
242+
if (!event.data || (!event.data.code && !event.data.error)) {
243+
return;
244+
}
245+
240246
// Accept messages from same origin (for localhost development and production)
241247
const redirectOrigin = new URL(this.config.redirectUri).origin;
242248
const currentOrigin = window.location.origin;

0 commit comments

Comments
 (0)