FEATURE: detect when client thinks user is logged on but is not

This cleans up an error condition where UI thinks a user is logged on
but the user is not. If this happens user will be prompted to refresh.
This commit is contained in:
Sam
2018-03-06 16:49:31 +11:00
parent f0d5f83424
commit 0134e41286
9 changed files with 82 additions and 11 deletions

View File

@@ -11,6 +11,7 @@
// Stuff we need to load first
//= require ./discourse/lib/utilities
//= require ./discourse/lib/page-visible
//= require ./discourse/lib/logout
//= require ./discourse/lib/ajax
//= require ./discourse/lib/text
//= require ./discourse/lib/hash

View File

@@ -1,5 +1,7 @@
import logout from 'discourse/lib/logout';
let _showingLogout = false;
// Subscribe to "logout" change events via the Message Bus
export default {
name: "logout",
@@ -7,14 +9,22 @@ export default {
initialize: function (container) {
const messageBus = container.lookup('message-bus:main');
const siteSettings = container.lookup('site-settings:main');
const keyValueStore = container.lookup('key-value-store:main');
if (!messageBus) { return; }
const callback = () => logout(siteSettings, keyValueStore);
messageBus.subscribe("/logout", function () {
bootbox.dialog(I18n.t("logout"), {label: I18n.t("refresh"), callback}, {onEscape: callback, backdrop: 'static'});
if (!_showingLogout) {
_showingLogout = true;
bootbox.dialog(I18n.t("logout"), {
label: I18n.t("refresh"),
callback: logout
}, {
onEscape: logout,
backdrop: 'static'
});
}
});
}
};

View File

@@ -1,7 +1,9 @@
import pageVisible from 'discourse/lib/page-visible';
import logout from 'discourse/lib/logout';
let _trackView = false;
let _transientHeader = null;
let _showingLogout = false;
export function setTransientHeader(key, value) {
_transientHeader = {key, value};
@@ -39,6 +41,10 @@ export function ajax() {
args.headers = args.headers || {};
if (Discourse.__container__.lookup('current-user:main')) {
args.headers['Discourse-Logged-In'] = "true";
}
if (_transientHeader) {
args.headers[_transientHeader.key] = _transientHeader.value;
_transientHeader = null;
@@ -54,7 +60,22 @@ export function ajax() {
args.headers['Discourse-Visible'] = "true";
}
let handleLogoff = function(xhr) {
if (xhr.getResponseHeader('Discourse-Logged-Out') && !_showingLogout) {
_showingLogout = true;
bootbox.dialog(
I18n.t("logout"), {label: I18n.t("refresh"), callback: logout},
{
onEscape: () => logout(),
backdrop: 'static'
}
);
}
};
args.success = (data, textStatus, xhr) => {
handleLogoff(xhr);
if (xhr.getResponseHeader('Discourse-Readonly')) {
Ember.run(() => Discourse.Site.currentProp('isReadOnly', true));
}
@@ -67,6 +88,8 @@ export function ajax() {
};
args.error = (xhr, textStatus, errorThrown) => {
handleLogoff(xhr);
// note: for bad CSRF we don't loop an extra request right away.
// this allows us to eliminate the possibility of having a loop.
if (xhr.status === 403 && xhr.responseText === "[\"BAD CSRF\"]") {

View File

@@ -1,4 +1,10 @@
export default function logout(siteSettings, keyValueStore) {
if (!siteSettings || !keyValueStore) {
const container = Discourse.__container__;
siteSettings = siteSettings || container.lookup('site-settings:main');
keyValueStore = keyValueStore || container.lookup('key-value-store:main');
}
keyValueStore.abandonLocal();
const redirect = siteSettings.logout_redirect;