Handle properly the user redirect after login.

This commit is contained in:
Andrey Antukh 2021-11-29 14:38:44 +01:00 committed by Alonso Torres
parent 2596ad27c3
commit 19944202fb
5 changed files with 67 additions and 61 deletions

View file

@ -150,7 +150,18 @@
;; --- EVENT: login ;; --- EVENT: login
(defn- logged-in (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] [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/reify ::logged-in (ptk/reify ::logged-in
IDeref IDeref
(-deref [_] profile) (-deref [_] profile)
@ -158,10 +169,9 @@
ptk/WatchEvent ptk/WatchEvent
(watch [_ _ _] (watch [_ _ _]
(when (is-authenticated? profile) (when (is-authenticated? profile)
(let [team-id (:default-team-id profile)]
(->> (rx/of (profile-fetched profile) (->> (rx/of (profile-fetched profile)
(fetch-teams) (fetch-teams)
(rt/nav' :dashboard-projects {:team-id team-id})) (get-redirect-event))
(rx/observe-on :async))))))) (rx/observe-on :async)))))))
(s/def ::login-params (s/def ::login-params
@ -215,11 +225,7 @@
{::ev/source "login"}))) {::ev/source "login"})))
(rx/tap on-success) (rx/tap on-success)
(rx/map logged-in) (rx/map logged-in)
(rx/observe-on :async))))) (rx/observe-on :async)))))))
ptk/EffectEvent
(effect [_ _ _]
(reset! storage {}))))
(defn login-from-token (defn login-from-token
[{:keys [profile] :as tdata}] [{:keys [profile] :as tdata}]
@ -247,12 +253,18 @@
(rx/map (fn [profile] (rx/map (fn [profile]
(with-meta profile (with-meta profile
{::ev/source "register"}))) {::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 ;; --- EVENT: logout
(defn logged-out (defn logged-out
[] ([] (logged-out {}))
([{:keys [capture-redirect?] :or {capture-redirect? false}}]
(ptk/reify ::logged-out (ptk/reify ::logged-out
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
@ -260,31 +272,33 @@
ptk/WatchEvent ptk/WatchEvent
(watch [_ _ _] (watch [_ _ _]
(rx/of (rt/nav :auth-login))) ;; 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 ptk/EffectEvent
(effect [_ _ _] (effect [_ state _]
(reset! storage {}) (when capture-redirect?
(i18n/reset-locale)))) (let [route (into {} (:route state))]
(reset! storage {::redirect-to route})))
(i18n/reset-locale)))))
(defn logout (defn logout
[] ([] (logout {}))
([params]
(ptk/reify ::logout (ptk/reify ::logout
ptk/WatchEvent ptk/WatchEvent
(watch [_ _ _] (watch [_ _ _]
(->> (rp/mutation :logout) (->> (rp/mutation :logout)
(rx/delay-at-least 300) (rx/delay-at-least 300)
(rx/catch (constantly (rx/of 1))) (rx/catch (constantly (rx/of 1)))
(rx/map logged-out))))) (rx/map #(logged-out params)))))))
;; --- EVENT: register ;; --- EVENT: register
;; TODO: remove
(s/def ::invitation-token ::us/not-empty-string)
(s/def ::register (s/def ::register
(s/keys :req-un [::fullname ::password ::email] (s/keys :req-un [::fullname ::password ::email]))
:opt-un [::invitation-token]))
(defn register (defn register
"Create a register event instance." "Create a register event instance."
@ -298,7 +312,11 @@
on-success identity}} (meta data)] on-success identity}} (meta data)]
(->> (rp/mutation :register-profile data) (->> (rp/mutation :register-profile data)
(rx/tap on-success) (rx/tap on-success)
(rx/catch on-error)))))) (rx/catch on-error))))
ptk/EffectEvent
(effect [_ _ _]
(swap! storage dissoc ::redirect-to))))
;; --- Update Profile ;; --- Update Profile

View file

@ -50,7 +50,7 @@
(defmethod ptk/handle-error :authentication (defmethod ptk/handle-error :authentication
[_] [_]
(let [msg (tr "errors.auth.unable-to-login")] (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))))) (ts/schedule 500 (st/emitf (dm/warn msg)))))

View file

@ -6,10 +6,9 @@
(ns app.main.ui.static (ns app.main.ui.static
(:require (:require
[app.main.data.users :as du]
[app.main.refs :as refs]
[app.main.store :as st] [app.main.store :as st]
[app.main.ui.icons :as i] [app.main.ui.icons :as i]
[app.util.globals :as globals]
[app.util.i18n :refer [tr]] [app.util.i18n :refer [tr]]
[app.util.object :as obj] [app.util.object :as obj]
[app.util.router :as rt] [app.util.router :as rt]
@ -19,13 +18,7 @@
{::mf/wrap-props false} {::mf/wrap-props false}
[props] [props]
(let [children (obj/get props "children") (let [children (obj/get props "children")
on-click (mf/use-callback on-click (mf/use-callback #(set! (.-href globals/location) ""))]
(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 {}))))))]
[:section.exception-layout [:section.exception-layout
[:div.exception-header [:div.exception-header

View file

@ -19,17 +19,16 @@
;; --- Router API ;; --- Router API
(defn map->Match
[data]
(r/map->Match data))
(defn resolve (defn resolve
([router id] (resolve router id {} {})) ([router id] (resolve router id {} {}))
([router id path-params] (resolve router id path-params {})) ([router id path-params] (resolve router id path-params {}))
([router id path-params query-params] ([router id path-params query-params]
(when-let [match (r/match-by-name router id path-params)] (when-let [match (r/match-by-name router id path-params)]
(if (empty? query-params) (r/match->path match query-params))))
(r/match->path match)
(let [query (u/map->query-string query-params)]
(-> (u/uri (r/match->path match))
(assoc :query query)
(str)))))))
(defn create (defn create
[routes] [routes]
@ -162,7 +161,3 @@
(e/unlistenByKey key))))) (e/unlistenByKey key)))))
(rx/take-until stoper) (rx/take-until stoper)
(rx/subs #(on-change router %))))))) (rx/subs #(on-change router %)))))))

View file

@ -3257,4 +3257,4 @@ msgid "errors.team-leave.insufficient-members"
msgstr "Insufficient members to leave team, you probably want to delete it." msgstr "Insufficient members to leave team, you probably want to delete it."
msgid "errors.auth.unable-to-login" 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."