Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message when window.opener == null #5340

Merged
merged 7 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions packages/@uppy/companion/src/server/controllers/send-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,33 @@ const htmlContent = (token, origin) => {
<head>
<meta charset="utf-8" />
<script>
window.opener.postMessage(${serialize({ token })}, ${serialize(origin)})
window.close()
(function() {
'use strict';

var data = ${serialize({ token })};
var origin = ${serialize(origin)};

Comment on lines +20 to +22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var data = ${serialize({ token })};
var origin = ${serialize(origin)};

if (window.opener != null) {
window.opener.postMessage(data, origin);
window.close();
} else {
// maybe this will work? (note that it's not possible to try/catch this to see whether it worked)
window.postMessage(data, origin);

console.warn('Unable to send the authentication token to the web app. This probably means that the web app was served from a HTTP server that includes the \`Cross-Origin-Opener-Policy: same-origin\` header. Make sure that the Uppy app is served from a server that does not send this header, or set to \`same-origin-allow-popups\`.');

addEventListener("DOMContentLoaded", function() {
document.body.appendChild(document.createTextNode('Something went wrong. Please contact the site administrator. You may now exit this page.'));
});
}
})();
</script>
</head>
<body></body>
<body>
<noscript>
JavaScript must be enabled for this to work.
</noscript>
</body>
</html>`
}

Expand Down
15 changes: 2 additions & 13 deletions packages/@uppy/companion/test/__tests__/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,8 @@ describe('test authentication callback', () => {
.get(`/dropbox/send-token?uppyAuthToken=${token}`)
.expect(200)
.expect((res) => {
const body = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script>
window.opener.postMessage({"token":"${token}"}, "http:\\u002F\\u002Flocalhost:3020")
window.close()
</script>
</head>
<body></body>
</html>`
expect(res.text).toBe(body)
expect(res.text).toMatch(`var data = {"token":"${token}"};`)
expect(res.text).toMatch(`var origin = "http:\\u002F\\u002Flocalhost:3020";`)
})
})
})