diff --git a/web/assets/auth-withssb-uri.js b/web/assets/auth-withssb-uri.js index 70f8778..fe2921a 100644 --- a/web/assets/auth-withssb-uri.js +++ b/web/assets/auth-withssb-uri.js @@ -5,22 +5,27 @@ const challengeElem = document.querySelector('#challenge'); const sc = challengeElem.dataset.sc; const evtSource = new EventSource(`/withssb/events?sc=${sc}`); +let otherTab; -ssbUriLink.addEventListener('click', (e) => { - errorElem.classList.add('hidden'); +ssbUriLink.onclick = function handleURI(ev) { + ev.preventDefault(); + const ssbUri = ssbUriLink.href; waitingElem.classList.remove('hidden'); -}); + otherTab = window.open(ssbUri, '_blank'); +}; evtSource.onerror = (e) => { waitingElem.classList.add('hidden'); errorElem.classList.remove('hidden'); console.error(e.data); + if (otherTab) otherTab.close(); }; evtSource.addEventListener('failed', (e) => { waitingElem.classList.add('hidden'); errorElem.classList.remove('hidden'); console.error(e.data); + if (otherTab) otherTab.close(); }); // prepare for the case that the success event happens while the browser is not on screen. @@ -32,11 +37,12 @@ window.addEventListener('blur', () => { evtSource.addEventListener('success', (e) => { waitingElem.classList.add('hidden'); evtSource.close(); - const redirectTo = `/withssb/finalize?token=${e.data}` + if (otherTab) otherTab.close(); + const redirectTo = `/withssb/finalize?token=${e.data}`; if (hasFocus) { window.location.replace(redirectTo); } else { - // wait for the browser to be back in focus and redirect then + // wait for the browser to be back in focus and redirect then window.addEventListener('focus', () => { window.location.replace(redirectTo); }); diff --git a/web/templates/auth/withssb_server_start.tmpl b/web/templates/auth/withssb_server_start.tmpl index 6a1e410..cebe25d 100644 --- a/web/templates/auth/withssb_server_start.tmpl +++ b/web/templates/auth/withssb_server_start.tmpl @@ -6,7 +6,6 @@ {{i18n "AuthWithSSBTitle"}}