From c726613eccb1859e16f61ca6f0b78223392fd88d Mon Sep 17 00:00:00 2001 From: BC Date: Fri, 1 Mar 2019 07:58:30 -0800 Subject: [PATCH] Kicks the user out when their session expires. --- docker-compose.yml | 1 + src/clj/auto_ap/routes/auth.clj | 2 +- src/cljs/auto_ap/effects.cljs | 32 +++++++++++++++---------- src/cljs/auto_ap/events.cljs | 5 ++-- src/cljs/auto_ap/views/pages/login.cljs | 6 +++++ 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index bb621e7e..d35685e7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -43,5 +43,6 @@ services: - DATOMIC_SQL_HOST=database ports: - 4334:4334 + restart: on-failure diff --git a/src/clj/auto_ap/routes/auth.clj b/src/clj/auto_ap/routes/auth.clj index a958e8ac..1a8e3141 100644 --- a/src/clj/auto_ap/routes/auth.clj +++ b/src/clj/auto_ap/routes/auth.clj @@ -46,7 +46,7 @@ (if (and token user) {:status 301 :headers {"Location" (str "/?jwt=" (jwt/sign (doto {:user (:name profile) - :exp (time/plus (time/now) (time/days 7)) + :exp (time/plus (time/now) (time/days 30)) :user/clients (map (fn [c] (dissoc c :client/bank-accounts )) (:user/clients user)) diff --git a/src/cljs/auto_ap/effects.cljs b/src/cljs/auto_ap/effects.cljs index f3a34e61..27c90edf 100644 --- a/src/cljs/auto_ap/effects.cljs +++ b/src/cljs/auto_ap/effects.cljs @@ -10,6 +10,7 @@ [clojure.walk :as walk] [venia.core :as v] [auto-ap.history :as p] + [auto-ap.events :as events] [pushy.core :as pushy])) (re-frame/reg-fx @@ -155,18 +156,25 @@ :body (when = (:post method) query) :url (str "/api/graphql?query=" (when (= :get method) (js/encodeURIComponent query)) "&variables=" (pr-str (or variables {})))}))] - (if (>= (:status response) 400) + (cond + (= (:status response) 401) + (re-frame/dispatch [::events/logout "Your session has expired. Please log in again."]) + + + + (>= (:status response) 400) (when on-error (->> response - :body - :errors - (dates->date-times) - (map #(assoc % :status (:status response))) - (conj on-error) - (re-frame/dispatch))) + :body + :errors + (dates->date-times) + (map #(assoc % :status (:status response))) + (conj on-error) + (re-frame/dispatch))) + :else (->> response - :body - :data - (dates->date-times) - (conj on-success) - (re-frame/dispatch))))))) + :body + :data + (dates->date-times) + (conj on-success) + (re-frame/dispatch))))))) diff --git a/src/cljs/auto_ap/events.cljs b/src/cljs/auto_ap/events.cljs index d01c3804..df1b7fbc 100644 --- a/src/cljs/auto_ap/events.cljs +++ b/src/cljs/auto_ap/events.cljs @@ -164,9 +164,10 @@ (re-frame/reg-event-fx ::logout - (fn [{:keys [db]} [_]] + (fn [{:keys [db]} [_ logout-reason]] {:db (assoc db :user nil :menu {:client {:active? false} - :account {:active? false}}) + :account {:active? false}} + :logout-reason logout-reason) :redirect (bidi/path-for routes/routes :login) :set-local-storage ["jwt" nil]})) diff --git a/src/cljs/auto_ap/views/pages/login.cljs b/src/cljs/auto_ap/views/pages/login.cljs index ccf09fcd..aaa8959a 100644 --- a/src/cljs/auto_ap/views/pages/login.cljs +++ b/src/cljs/auto_ap/views/pages/login.cljs @@ -9,12 +9,18 @@ [auto-ap.routes :as routes] [bidi.bidi :as bidi])) +(re-frame/reg-sub + ::logout-reason + (fn [db] (-> db :logout-reason))) + (defn login-page [] [:div.container [:section.is-fullheight.hero [:div.hero-body [:div.container [:div.column.is-4.is-offset-4.has-text-centered + (when-let [reason @(re-frame/subscribe [::logout-reason])] + [:div.notification.is-warning reason]) [:h1.title "Login"] [:div.box.slideInFromBelow