Skip to content

Commit

Permalink
close es on logout , 401, and empty token case
Browse files Browse the repository at this point in the history
  • Loading branch information
nohaapav committed Nov 19, 2017
1 parent db65df7 commit 8528553
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/cljs/swarmpit/component/header.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[material.icon :as icon]
[swarmpit.url :refer [dispatch!]]
[swarmpit.storage :as storage]
[swarmpit.event.source :as eventsource]
[swarmpit.routes :as routes]
[clojure.string :as string]
[rum.core :as rum]
Expand Down Expand Up @@ -61,6 +62,7 @@
{:key "user-menu-logout"
:onTouchTap (fn []
(storage/remove "token")
(eventsource/close!)
(dispatch!
(routes/path-for-frontend :login)))
:primaryText "Log out"})))
Expand Down
11 changes: 9 additions & 2 deletions src/cljs/swarmpit/controller.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@
[cemerick.url :refer [query->map]]
[swarmpit.url :refer [dispatch!]]
[swarmpit.storage :as storage]
[swarmpit.event.source :as eventsource]
[swarmpit.routes :as routes]
[swarmpit.component.state :as state]))

(def cursor [:route])

(defmulti dispatch (fn [location] (:handler location)))

(defn login!
"Close event stream and redirect to login page"
[]
(eventsource/close!)
(dispatch {:handler :login})
(dispatch! (routes/path-for-frontend :login)))

(defn- execute
([success-fx error-fx]
{:headers {"Authorization" (storage/get "token")}
Expand All @@ -27,8 +35,7 @@
([api success-fx]
(get api success-fx (fn [{:keys [status]}]
(case status
401 (dispatch!
(routes/path-for-frontend :login))
401 (login!)
403 (dispatch {:handler :unauthorized})
404 (dispatch {:handler :not-found})
(dispatch {:handler :error})))))
Expand Down
7 changes: 1 addition & 6 deletions src/cljs/swarmpit/router.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@

(defonce location (atom nil))

(defn- dispatch-login!
[]
(controller/dispatch {:handler :login})
(dispatch! (routes/path-for-frontend :login)))

(defn- on-navigate
[location]
(if (nil? (storage/get "token"))
(dispatch-login!)
(controller/login!)
;; Render to service list by default as we don't have any index page right now
(if (= :index (:handler location))
(controller/dispatch {:handler :service-list})
Expand Down

0 comments on commit 8528553

Please sign in to comment.