From 11b73e1fb7c7095fe0310558c5efeb28bfb83be2 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 1 Sep 2015 12:44:31 -0400 Subject: [PATCH] Avoid calling `Discourse.logout` and use an action instead --- app/assets/javascripts/discourse.js | 20 --------------- .../discourse/components/user-menu.js.es6 | 2 +- .../discourse/initializers/logout.js.es6 | 13 +++------- .../discourse/lib/key_value_store.js | 2 -- .../javascripts/discourse/lib/logout.js.es6 | 10 ++++++++ .../javascripts/discourse/models/user.js.es6 | 25 +++---------------- .../discourse/routes/application.js.es6 | 6 +++++ .../javascripts/discourse/routes/user.js.es6 | 4 --- .../discourse/templates/header.hbs | 2 +- 9 files changed, 26 insertions(+), 58 deletions(-) create mode 100644 app/assets/javascripts/discourse/lib/logout.js.es6 diff --git a/app/assets/javascripts/discourse.js b/app/assets/javascripts/discourse.js index 4e2c45d63..57165d9d2 100644 --- a/app/assets/javascripts/discourse.js +++ b/app/assets/javascripts/discourse.js @@ -78,26 +78,6 @@ window.Discourse = Ember.Application.createWithMixins(Discourse.Ajax, { this.set('notifyCount', count); }, - /** - Log the current user out of Discourse - - @method logout - **/ - logout: function() { - Discourse.User.logout().then(function() { - // Reloading will refresh unbound properties - Discourse.KeyValueStore.abandonLocal(); - - var redirect = Discourse.SiteSettings.logout_redirect; - if(redirect.length === 0){ - window.location.pathname = Discourse.getURL('/'); - } else { - window.location.href = redirect; - } - - }); - }, - authenticationComplete: function(options) { // TODO, how to dispatch this to the controller without the container? var loginController = Discourse.__container__.lookup('controller:login'); diff --git a/app/assets/javascripts/discourse/components/user-menu.js.es6 b/app/assets/javascripts/discourse/components/user-menu.js.es6 index acd75d360..1965640d9 100644 --- a/app/assets/javascripts/discourse/components/user-menu.js.es6 +++ b/app/assets/javascripts/discourse/components/user-menu.js.es6 @@ -71,7 +71,7 @@ export default Ember.Component.extend({ }); }, logout() { - Discourse.logout(); + this.sendAction('logoutAction'); } } }); diff --git a/app/assets/javascripts/discourse/initializers/logout.js.es6 b/app/assets/javascripts/discourse/initializers/logout.js.es6 index 7fdf60ae1..bc3fe49e8 100644 --- a/app/assets/javascripts/discourse/initializers/logout.js.es6 +++ b/app/assets/javascripts/discourse/initializers/logout.js.es6 @@ -1,3 +1,5 @@ +import logout from 'discourse/lib/logout'; + // Subscribe to "logout" change events via the Message Bus export default { name: "logout", @@ -8,17 +10,10 @@ export default { siteSettings = container.lookup('site-settings:main'); if (!messageBus) { return; } + const callback = () => logout(siteSettings); messageBus.subscribe("/logout", function () { - var refresher = function() { - var redirect = siteSettings.logout_redirect; - if(redirect.length === 0){ - window.location.pathname = Discourse.getURL('/'); - } else { - window.location.href = redirect; - } - }; - bootbox.dialog(I18n.t("logout"), {label: I18n.t("refresh"), callback: refresher}, {onEscape: refresher, backdrop: 'static'}); + bootbox.dialog(I18n.t("logout"), {label: I18n.t("refresh"), callback}, {onEscape: callback, backdrop: 'static'}); }); } }; diff --git a/app/assets/javascripts/discourse/lib/key_value_store.js b/app/assets/javascripts/discourse/lib/key_value_store.js index ecb1651eb..2ae20f5e6 100644 --- a/app/assets/javascripts/discourse/lib/key_value_store.js +++ b/app/assets/javascripts/discourse/lib/key_value_store.js @@ -5,8 +5,6 @@ @namespace Discourse @module Discourse **/ - - var safeLocalStorage; try { diff --git a/app/assets/javascripts/discourse/lib/logout.js.es6 b/app/assets/javascripts/discourse/lib/logout.js.es6 new file mode 100644 index 000000000..8dc0539b8 --- /dev/null +++ b/app/assets/javascripts/discourse/lib/logout.js.es6 @@ -0,0 +1,10 @@ +export default function logout(siteSettings) { + Discourse.KeyValueStore.abandonLocal(); + + const redirect = siteSettings.logout_redirect; + if (Ember.isEmpty(redirect)) { + window.location.pathname = Discourse.getURL('/'); + } else { + window.location.href = redirect; + } +} diff --git a/app/assets/javascripts/discourse/models/user.js.es6 b/app/assets/javascripts/discourse/models/user.js.es6 index 19cf8eace..3f5941bc7 100644 --- a/app/assets/javascripts/discourse/models/user.js.es6 +++ b/app/assets/javascripts/discourse/models/user.js.es6 @@ -26,14 +26,12 @@ const User = RestModel.extend({ return UserPostsStream.create({ user: this }); }.property(), - /** - Is this user a member of staff? - - @property staff - @type {Boolean} - **/ staff: Em.computed.or('admin', 'moderator'), + destroySession() { + return Discourse.ajax(`/session/${this.get('username')}`, { type: 'DELETE'}); + }, + searchContext: function() { return { type: 'user', @@ -449,21 +447,6 @@ User.reopenClass(Singleton, { return null; }, - /** - Logs out the currently logged in user - - @method logout - @returns {Promise} resolved when the logout finishes - **/ - logout: function() { - var discourseUserClass = this; - return Discourse.ajax("/session/" + Discourse.User.currentProp('username'), { - type: 'DELETE' - }).then(function () { - discourseUserClass.currentUser = null; - }); - }, - /** Checks if given username is valid for this email address diff --git a/app/assets/javascripts/discourse/routes/application.js.es6 b/app/assets/javascripts/discourse/routes/application.js.es6 index 0b66dbdcd..443d0e9c8 100644 --- a/app/assets/javascripts/discourse/routes/application.js.es6 +++ b/app/assets/javascripts/discourse/routes/application.js.es6 @@ -1,4 +1,5 @@ import { setting } from 'discourse/lib/computed'; +import logout from 'discourse/lib/logout'; import showModal from 'discourse/lib/show-modal'; import OpenComposer from "discourse/mixins/open-composer"; @@ -16,6 +17,11 @@ const ApplicationRoute = Discourse.Route.extend(OpenComposer, { siteTitle: setting('title'), actions: { + + logout() { + this.currentUser.destroySession().then(() => logout(this.siteSettings)); + }, + _collectTitleTokens(tokens) { tokens.push(this.get('siteTitle')); Discourse.set('_docTitle', tokens.join(' - ')); diff --git a/app/assets/javascripts/discourse/routes/user.js.es6 b/app/assets/javascripts/discourse/routes/user.js.es6 index cf7ea55c0..c0d4507ad 100644 --- a/app/assets/javascripts/discourse/routes/user.js.es6 +++ b/app/assets/javascripts/discourse/routes/user.js.es6 @@ -11,10 +11,6 @@ export default Discourse.Route.extend({ }, actions: { - logout() { - Discourse.logout(); - }, - composePrivateMessage(user, post) { const recipient = user ? user.get('username') : '', reply = post ? window.location.protocol + "//" + window.location.host + post.get("url") : null; diff --git a/app/assets/javascripts/discourse/templates/header.hbs b/app/assets/javascripts/discourse/templates/header.hbs index effeb6994..d09c4209f 100644 --- a/app/assets/javascripts/discourse/templates/header.hbs +++ b/app/assets/javascripts/discourse/templates/header.hbs @@ -1,4 +1,4 @@ -{{user-menu visible=userMenuVisible}} +{{user-menu visible=userMenuVisible logoutAction="logout"}} {{hamburger-menu visible=hamburgerVisible showKeyboardAction="showKeyboardShortcutsHelp"}} {{search-menu visible=searchVisible}}