diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index cf72ba248..a739bc7d3 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -1215,6 +1215,21 @@ define([ } if (parsedNew) { oldHref = newHref; } }; + // Listen for login/logout in other tabs + window.addEventListener('storage', function (e) { + if (e.key !== common.userHashKey) { return; } + var o = e.oldValue; + var n = e.newValue; + if (!o && n) { + document.location.reload(); + } else if (o && !n) { + common.logout(); + if (getNetwork()) { + getNetwork().disconnect(); + } + } + }); + if (PINNING_ENABLED && isLoggedIn()) { console.log("logged in. pads will be pinned"); diff --git a/www/common/fsStore.js b/www/common/fsStore.js index 6440bc949..db04f2907 100644 --- a/www/common/fsStore.js +++ b/www/common/fsStore.js @@ -304,28 +304,6 @@ define([ var exp = {}; - window.addEventListener('storage', function (e) { - if (e.key !== Cryptpad.userHashKey) { return; } - var o = e.oldValue; - var n = e.newValue; - if (!o && n) { - window.location.reload(); - } else if (o && !n) { - $(window).on('keyup', function (e) { - if (e.keyCode === 27) { - //UI.removeLoadingScreen(); - } - }); - Cryptpad.logout(); - UI.alert(Cryptpad.Messages.onLogout, null, true); - //UI.addLoadingScreen({hideTips: true}); - //UI.errorLoadingScreen(Cryptpad.Messages.onLogout, true); - if (exp.info) { - exp.info.network.disconnect(); - } - } - }); - var rt = window.rt = Listmap.create(listmapConfig); exp.realtime = rt.realtime; diff --git a/www/common/sframe-app-framework.js b/www/common/sframe-app-framework.js index c25f11fee..c1e74d0b3 100644 --- a/www/common/sframe-app-framework.js +++ b/www/common/sframe-app-framework.js @@ -440,7 +440,7 @@ define([ }); }, 2000); - //Cryptpad.onLogout(function () { ... }); + //common.onLogout(function () { ... }); Cryptpad.onError(function (info) { if (info && info.type === "store") { diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index b7caec948..d0bf74c59 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -101,6 +101,7 @@ define([ secret.channel = Utils.Hash.createChannelId(); } Cryptpad.getShareHashes(secret, waitFor(function (err, h) { hashes = h; })); + }).nThen(function () { var readOnly = secret.keys && !secret.keys.editKeyStr; if (!secret.keys) { secret.keys = secret.key; } @@ -161,6 +162,10 @@ define([ sframeChan.onReg('EV_METADATA_UPDATE', updateMeta); proxy.on('change', 'settings', updateMeta); + Cryptpad.onLogout(function () { + sframeChan.event('EV_LOGOUT'); + }); + Cryptpad.onError(function (info) { console.log('error'); console.log(info); diff --git a/www/common/sframe-common.js b/www/common/sframe-common.js index d638df897..99c1356aa 100644 --- a/www/common/sframe-common.js +++ b/www/common/sframe-common.js @@ -314,6 +314,14 @@ define([ funcs.whenRealtimeSyncs = evRealtimeSynced.reg; + var logoutHandlers = []; + funcs.onLogout = function (h) { + if (typeof (h) !== "function") { return; } + if (logoutHandlers.indexOf(h) !== -1) { return; } + logoutHandlers.push(h); + }; + + Object.freeze(funcs); return { create: function (cb) { @@ -360,6 +368,19 @@ define([ UI.addTooltips(); + ctx.sframeChan.on('EV_LOGOUT', function () { + $(window).on('keyup', function (e) { + if (e.keyCode === 27) { + UI.removeLoadingScreen(); + } + }); + UI.addLoadingScreen({hideTips: true}); + UI.errorLoadingScreen(Messages.onLogout, true); + logoutHandlers.forEach(function (h) { + if (typeof (h) === "function") { h(); } + }); + }); + ctx.sframeChan.on('EV_RT_CONNECT', function () { CommonRealtime.setConnectionState(true); }); ctx.sframeChan.on('EV_RT_DISCONNECT', function () { CommonRealtime.setConnectionState(false); }); diff --git a/www/common/sframe-protocol.js b/www/common/sframe-protocol.js index 120dade8d..0aedccde1 100644 --- a/www/common/sframe-protocol.js +++ b/www/common/sframe-protocol.js @@ -56,6 +56,8 @@ define({ // Log the user out in all the tabs 'Q_LOGOUT': true, + // Tell the user that he has been logged out from outside (probably from another tab) + 'EV_LOGOUT': true, // When moving to the login or register page from a pad, we need to redirect to that pad at the // end of the login process. This query set the current href to the sessionStorage. diff --git a/www/common/toolbar3.js b/www/common/toolbar3.js index e488d6382..45c72aea0 100644 --- a/www/common/toolbar3.js +++ b/www/common/toolbar3.js @@ -1038,7 +1038,7 @@ define([ initClickEvents(toolbar, config); initNotifications(toolbar, config); - toolbar.failed = function () { + var failed = toolbar.failed = function () { toolbar.connected = false; if (toolbar.spinner) { @@ -1079,12 +1079,11 @@ define([ }; // On log out, remove permanently the realtime elements of the toolbar - // TODO - /*Common.onLogout(function () { + Common.onLogout(function () { failed(); if (toolbar.useradmin) { toolbar.useradmin.hide(); } if (toolbar.userlist) { toolbar.userlist.hide(); } - });*/ + }); return toolbar; }; diff --git a/www/drive/inner.js b/www/drive/inner.js index 862a647ae..2d106ec8a 100644 --- a/www/drive/inner.js +++ b/www/drive/inner.js @@ -3057,7 +3057,7 @@ define([ onConnectError(); } }); - //Cryptpad.onLogout(function () { setEditable(false); }); + common.onLogout(function () { setEditable(false); }); }); }; main(); diff --git a/www/whiteboard/inner.js b/www/whiteboard/inner.js index af02b4ec3..dbea86151 100644 --- a/www/whiteboard/inner.js +++ b/www/whiteboard/inner.js @@ -629,7 +629,7 @@ define([ saveImage(); }); - Cryptpad.onLogout(function () { setEditable(false); }); + common.onLogout(function () { setEditable(false); }); }; var main = function () {