improve sign-in with ssb JS for Firefox

This commit is contained in:
Andre Staltz 2021-04-22 17:14:17 +03:00 committed by Henry
parent 3beb656b9c
commit 2f0afe8f33
2 changed files with 11 additions and 6 deletions

View File

@ -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);
});

View File

@ -6,7 +6,6 @@
<a
id="start-auth-uri"
href="{{.SSBURI}}"
target="_blank"
class="shadow rounded flex flex-row justify-center items-center mt-8 px-4 h-8 text-gray-100 bg-green-500 hover:bg-green-600 focus:outline-none focus:ring-2 focus:ring-green-600 focus:ring-opacity-50"
>{{i18n "AuthWithSSBTitle"}}</a>