diff --git a/frontend/src/app/main/data/users.cljs b/frontend/src/app/main/data/users.cljs index afd344dc6..58526ae30 100644 --- a/frontend/src/app/main/data/users.cljs +++ b/frontend/src/app/main/data/users.cljs @@ -150,18 +150,28 @@ ;; --- EVENT: login (defn- logged-in + "This is the main event that is executed once we have logged in + profile. The profile can proceed from standard login or from + accepting invitation, or third party auth signup or singin." [profile] - (ptk/reify ::logged-in - IDeref - (-deref [_] profile) + (letfn [(get-redirect-event [] + (if-let [{:keys [data path-params query-params]} (::redirect-to @storage)] + (do + (swap! storage dissoc ::redirect-to) + (rt/nav' (:name data) path-params query-params)) + (let [team-id (:default-team-id profile)] + (rt/nav' :dashboard-projects {:team-id team-id}))))] - ptk/WatchEvent - (watch [_ _ _] - (when (is-authenticated? profile) - (let [team-id (:default-team-id profile)] + (ptk/reify ::logged-in + IDeref + (-deref [_] profile) + + ptk/WatchEvent + (watch [_ _ _] + (when (is-authenticated? profile) (->> (rx/of (profile-fetched profile) (fetch-teams) - (rt/nav' :dashboard-projects {:team-id team-id})) + (get-redirect-event)) (rx/observe-on :async))))))) (s/def ::login-params @@ -215,11 +225,7 @@ {::ev/source "login"}))) (rx/tap on-success) (rx/map logged-in) - (rx/observe-on :async))))) - - ptk/EffectEvent - (effect [_ _ _] - (reset! storage {})))) + (rx/observe-on :async))))))) (defn login-from-token [{:keys [profile] :as tdata}] @@ -247,44 +253,52 @@ (rx/map (fn [profile] (with-meta profile {::ev/source "register"}))) - (rx/map logged-in)))))) + (rx/map logged-in) + (rx/observe-on :async)))) + + ptk/EffectEvent + (effect [_ _ _] + (swap! storage dissoc ::redirect-to)))) ;; --- EVENT: logout (defn logged-out - [] - (ptk/reify ::logged-out - ptk/UpdateEvent - (update [_ state] - (select-keys state [:route :router :session-id :history])) + ([] (logged-out {})) + ([{:keys [capture-redirect?] :or {capture-redirect? false}}] + (ptk/reify ::logged-out + ptk/UpdateEvent + (update [_ state] + (select-keys state [:route :router :session-id :history])) - ptk/WatchEvent - (watch [_ _ _] - (rx/of (rt/nav :auth-login))) + ptk/WatchEvent + (watch [_ _ _] + ;; NOTE: We need the `effect` of the current event to be + ;; executed before the redirect. + (->> (rx/of (rt/nav :auth-login)) + (rx/observe-on :async))) - ptk/EffectEvent - (effect [_ _ _] - (reset! storage {}) - (i18n/reset-locale)))) + ptk/EffectEvent + (effect [_ state _] + (when capture-redirect? + (let [route (into {} (:route state))] + (reset! storage {::redirect-to route}))) + (i18n/reset-locale))))) (defn logout - [] - (ptk/reify ::logout - ptk/WatchEvent - (watch [_ _ _] - (->> (rp/mutation :logout) - (rx/delay-at-least 300) - (rx/catch (constantly (rx/of 1))) - (rx/map logged-out))))) + ([] (logout {})) + ([params] + (ptk/reify ::logout + ptk/WatchEvent + (watch [_ _ _] + (->> (rp/mutation :logout) + (rx/delay-at-least 300) + (rx/catch (constantly (rx/of 1))) + (rx/map #(logged-out params))))))) ;; --- EVENT: register -;; TODO: remove -(s/def ::invitation-token ::us/not-empty-string) - (s/def ::register - (s/keys :req-un [::fullname ::password ::email] - :opt-un [::invitation-token])) + (s/keys :req-un [::fullname ::password ::email])) (defn register "Create a register event instance." @@ -298,7 +312,11 @@ on-success identity}} (meta data)] (->> (rp/mutation :register-profile data) (rx/tap on-success) - (rx/catch on-error)))))) + (rx/catch on-error)))) + + ptk/EffectEvent + (effect [_ _ _] + (swap! storage dissoc ::redirect-to)))) ;; --- Update Profile diff --git a/frontend/src/app/main/errors.cljs b/frontend/src/app/main/errors.cljs index dc65ebdc6..3b1213332 100644 --- a/frontend/src/app/main/errors.cljs +++ b/frontend/src/app/main/errors.cljs @@ -50,7 +50,7 @@ (defmethod ptk/handle-error :authentication [_] (let [msg (tr "errors.auth.unable-to-login")] - (st/emit! (du/logout)) + (st/emit! (du/logout {:capture-redirect? true})) (ts/schedule 500 (st/emitf (dm/warn msg))))) diff --git a/frontend/src/app/main/ui/static.cljs b/frontend/src/app/main/ui/static.cljs index 3ada96ac1..79e9b11df 100644 --- a/frontend/src/app/main/ui/static.cljs +++ b/frontend/src/app/main/ui/static.cljs @@ -6,10 +6,9 @@ (ns app.main.ui.static (:require - [app.main.data.users :as du] - [app.main.refs :as refs] [app.main.store :as st] [app.main.ui.icons :as i] + [app.util.globals :as globals] [app.util.i18n :refer [tr]] [app.util.object :as obj] [app.util.router :as rt] @@ -19,13 +18,7 @@ {::mf/wrap-props false} [props] (let [children (obj/get props "children") - on-click (mf/use-callback - (fn [] - (let [profile (deref refs/profile)] - (if (du/is-authenticated? profile) - (let [team-id (:default-team-id profile)] - (st/emit! (rt/nav :dashboard-projects {:team-id team-id}))) - (st/emit! (rt/nav :auth-login {}))))))] + on-click (mf/use-callback #(set! (.-href globals/location) ""))] [:section.exception-layout [:div.exception-header diff --git a/frontend/src/app/util/router.cljs b/frontend/src/app/util/router.cljs index 3bbe5eeec..21a89434c 100644 --- a/frontend/src/app/util/router.cljs +++ b/frontend/src/app/util/router.cljs @@ -19,17 +19,16 @@ ;; --- Router API +(defn map->Match + [data] + (r/map->Match data)) + (defn resolve ([router id] (resolve router id {} {})) ([router id path-params] (resolve router id path-params {})) ([router id path-params query-params] (when-let [match (r/match-by-name router id path-params)] - (if (empty? query-params) - (r/match->path match) - (let [query (u/map->query-string query-params)] - (-> (u/uri (r/match->path match)) - (assoc :query query) - (str))))))) + (r/match->path match query-params)))) (defn create [routes] @@ -162,7 +161,3 @@ (e/unlistenByKey key))))) (rx/take-until stoper) (rx/subs #(on-change router %))))))) - - - - diff --git a/frontend/translations/en.po b/frontend/translations/en.po index 6f8729c87..bd9542144 100644 --- a/frontend/translations/en.po +++ b/frontend/translations/en.po @@ -3257,4 +3257,4 @@ msgid "errors.team-leave.insufficient-members" msgstr "Insufficient members to leave team, you probably want to delete it." msgid "errors.auth.unable-to-login" -msgstr "Unable to login, looks like you are not authenticated." +msgstr "Looks like you are not authenticated or session expired."