Skip to content

Commit

Permalink
Detect when we can't post between tabs and show error message (#2182)
Browse files Browse the repository at this point in the history
  • Loading branch information
robknight and rrrliu authored Dec 6, 2024
1 parent bddb8c2 commit 1a2ece9
Showing 1 changed file with 44 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ const Header = styled.div`
align-items: center;
`;

/**
* Throws an error that indicates that the current window does not have an
* opener, and that the user should try again using a different browser.
*
* If the user requests a proof to be returned via `postMessage`, then the
* current window should have an opener - the window that opened the Zupass
* popup. However, some browsers seem not to set the `opener` property, so
* we need to check for that and throw an error if it's not set.
*
* To date, this seems to occur in in-app browsers like Metamask. However, it
* has also been reported on Chrome, although we're not yet sure what browser
* settings/extensions may be causing it.
*
* See "onProveCallback" below for more context.
*/
function throwNoOpenerError(): never {
throw new Error(
"Zupass was unable to send your proof. Please retry this on a different browser or device."
);
}

/**
* Renders a UI in response to a request from Zupass to calculate
* a particular PCD. For arguments which are filled in by the requester
Expand Down Expand Up @@ -53,23 +74,36 @@ export function GenericProveScreen({
multiplePCDs?: SerializedPCD[]
) => {
if (pendingPCD) {
if (window.opener && req.postMessage) {
postPendingPCDMessage(window.opener, pendingPCD);
window.close();
if (req.postMessage) {
if (window.opener) {
postPendingPCDMessage(window.opener, pendingPCD);
window.close();
} else {
// Opener doesn't exist, so we can't send the proof.
throwNoOpenerError();
}
}
safeRedirectPending(req.returnUrl, pendingPCD);
} else if (multiplePCDs !== undefined) {
if (window.opener && req.postMessage) {
postSerializedMultiPCDMessage(window.opener, multiplePCDs);
window.close();
if (req.postMessage) {
if (window.opener) {
postSerializedMultiPCDMessage(window.opener, multiplePCDs);
window.close();
} else {
throwNoOpenerError();
}
}
safeRedirect(req.returnUrl, undefined, multiplePCDs);
} else {
if (window.opener && req.postMessage) {
if (serialized) {
postSerializedPCDMessage(window.opener, serialized);
if (req.postMessage) {
if (window.opener) {
if (serialized) {
postSerializedPCDMessage(window.opener, serialized);
}
window.close();
} else {
throwNoOpenerError();
}
window.close();
}
safeRedirect(req.returnUrl, serialized);
}
Expand Down

0 comments on commit 1a2ece9

Please sign in to comment.