]> git.immae.eu Git - github/bastienwirtz/homer.git/blobdiff - src/components/ConnectivityChecker.vue
fix(auth): add timestamp in URL to prevent infinite redirection loop
[github/bastienwirtz/homer.git] / src / components / ConnectivityChecker.vue
index 02cbd7fcd4c45d58888cc8f99bb88e00661c18c3..1c677d24a068c1ef742ea7060027a786010702ee 100644 (file)
@@ -17,6 +17,9 @@ export default {
     };
   },
   created: function () {
+    if (/t=\d+/.test(window.location.href)) {
+      window.history.replaceState({}, document.title, window.location.pathname);
+    }
     let that = this;
     this.checkOffline();
 
@@ -29,15 +32,40 @@ export default {
       },
       false
     );
+    window.addEventListener(
+      "online",
+      function () {
+          that.checkOffline();
+      },
+      false
+    );
+    window.addEventListener(
+      "offline",
+      function () {
+        this.offline = true;
+      },
+      false
+    );
   },
   methods: {
     checkOffline: function () {
+      if (!navigator.onLine) {
+        this.offline = true;
+        return;
+      }
+
+      // extra check to make sure we're not offline
       let that = this;
       return fetch(window.location.href + "?alive", {
         method: "HEAD",
         cache: "no-store",
+        redirect: "manual"
       })
         .then(function (response) {
+          // opaqueredirect means request has been redirected, to auth provider probably
+          if (response.type === "opaqueredirect" && !response.ok) {
+            window.location.href = window.location.href + "?t="+(new Date().valueOf());
+          }
           that.offline = !response.ok;
         })
         .catch(function () {