From cdf8dc043a2b42200e28e83150950b24b39202b1 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Thu, 27 Dec 2018 11:30:20 +0200 Subject: [PATCH] [#7192] fix autologin switch behaviour on wrong password Signed-off-by: Igor Mandrigin --- src/status_im/accounts/login/core.cljs | 122 ++++++++++++++----------- src/status_im/events.cljs | 2 +- src/status_im/ui/screens/views.cljs | 4 +- 3 files changed, 74 insertions(+), 54 deletions(-) diff --git a/src/status_im/accounts/login/core.cljs b/src/status_im/accounts/login/core.cljs index f22f4e616e..04b0656d1d 100644 --- a/src/status_im/accounts/login/core.cljs +++ b/src/status_im/accounts/login/core.cljs @@ -25,7 +25,7 @@ ;; - `:callback/login` event is dispatched, account is changed in datastore, web-data is cleared ;; - `:init.callback/account-change-success` event is dispatched -(defn login! [address password save-password?] +(defn login! [address password] (status/login address password #(re-frame/dispatch [:accounts.login.callback/login-success %]))) (defn verify! [address password realm-error] @@ -48,8 +48,8 @@ ;;;; Handlers (fx/defn login [cofx] - (let [{:keys [address password save-password?]} (accounts.db/credentials cofx)] - {:accounts.login/login [address password save-password?]})) + (let [{:keys [address password]} (accounts.db/credentials cofx)] + {:accounts.login/login [address password]})) (fx/defn initialize-wallet [cofx] (fx/merge cofx @@ -58,17 +58,14 @@ (transactions/start-sync))) (fx/defn user-login [{:keys [db] :as cofx} create-database?] - (let [{:keys [address password save-password?]} (accounts.db/credentials cofx)] + (let [{:keys [address password]} (accounts.db/credentials cofx)] (fx/merge cofx - (merge - {:db (-> db - (assoc-in [:accounts/login :processing] true) - (assoc :node/on-ready :login)) - :accounts.login/clear-web-data nil - :data-store/change-account [address password create-database?]} - (when save-password? - {:keychain/save-user-password [address password]}))))) + {:db (-> db + (assoc-in [:accounts/login :processing] true) + (assoc :node/on-ready :login)) + :accounts.login/clear-web-data nil + :data-store/change-account [address password create-database?]}))) (fx/defn account-and-db-password-do-not-match [{:keys [db] :as cofx} error] @@ -76,6 +73,7 @@ cofx {:db (update db :accounts/login assoc + :save-password? false :error error :processing false) @@ -90,7 +88,8 @@ (let [data (types/json->clj login-result) error (:error data) success (empty? error) - address (get-in db [:account/account :address])] + {:keys [address password save-password?]} + (accounts.db/credentials cofx)] ;; check if logged into account (when address (if success @@ -101,6 +100,9 @@ :web3/fetch-node-version [web3 #(re-frame/dispatch [:web3/fetch-node-version-callback %])]} + (fn [_] + (when save-password? + {:keychain/save-user-password [address password]})) (protocol/initialize-protocol) #(when-not platform/desktop? (initialize-wallet %))) @@ -143,50 +145,68 @@ :error error :processing false)}))))) +(fx/defn migrations-failed + [{:keys [db]} {:keys [realm-error erase-button]}] + (let [{:keys [message details]} realm-error + address (get-in db [:accounts/login :address])] + {:ui/show-confirmation + {:title (i18n/label :migrations-failed-title) + :content (i18n/label + :migrations-failed-content + (merge + {:message message + :erase-accounts-data-button-text erase-button} + details)) + :confirm-button-text erase-button + :on-cancel #(re-frame/dispatch + [:init.ui/data-reset-cancelled ""]) + :on-accept #(re-frame/dispatch + [:init.ui/account-data-reset-accepted address])}})) + +(fx/defn verify-account + [{:keys [db] :as cofx} {:keys [realm-error]}] + (fx/merge cofx + {:db (-> db + (assoc :node/on-ready :verify-account) + (assoc :realm-error realm-error))} + (node/initialize nil))) + +(fx/defn unknown-realm-error + [cofx {:keys [realm-error erase-button]}] + (let [{:keys [message]} realm-error + {:keys [address]} (accounts.db/credentials cofx)] + {:ui/show-confirmation + {:title (i18n/label :unknown-realm-error) + :content (i18n/label + :unknown-realm-error-content + {:message message + :erase-accounts-data-button-text erase-button}) + :confirm-button-text (i18n/label :invalid-key-confirm) + :on-cancel #(re-frame/dispatch + [:init.ui/data-reset-cancelled ""]) + :on-accept #(re-frame/dispatch + [:init.ui/account-data-reset-accepted address])}})) + (fx/defn handle-change-account-error [{:keys [db] :as cofx} error] - (let [{:keys [error message details] :as realm-error} + (let [{:keys [error] :as realm-error} (if (map? error) error {:message (str error)}) - {:keys [address]} (accounts.db/credentials cofx) erase-button (i18n/label :migrations-erase-accounts-data-button)] - (case error - :migrations-failed - (let [{:keys [message]} realm-error - address (get-in db [:accounts/login :address])] - {:ui/show-confirmation - {:title (i18n/label :migrations-failed-title) - :content (i18n/label - :migrations-failed-content - (merge - {:message message - :erase-accounts-data-button-text erase-button} - details)) - :confirm-button-text erase-button - :on-cancel #(re-frame/dispatch - [:init.ui/data-reset-cancelled ""]) - :on-accept #(re-frame/dispatch - [:init.ui/account-data-reset-accepted address])}}) + (fx/merge + cofx + {:db (assoc-in db [:accounts/login :save-password?] false)} + (case error + :migrations-failed + (migrations-failed {:realm-error realm-error + :erase-button erase-button}) - (:database-does-not-exist :decryption-failed) - (fx/merge cofx - {:db (-> db - (assoc :node/on-ready :verify-account) - (assoc :realm-error realm-error))} - (node/initialize nil)) + (:database-does-not-exist :decryption-failed) + (verify-account {:realm-error realm-error}) - {:ui/show-confirmation - {:title (i18n/label :unknown-realm-error) - :content (i18n/label - :unknown-realm-error-content - {:message message - :erase-accounts-data-button-text erase-button}) - :confirm-button-text (i18n/label :invalid-key-confirm) - :on-cancel #(re-frame/dispatch - [:init.ui/data-reset-cancelled ""]) - :on-accept #(re-frame/dispatch - [:init.ui/account-data-reset-accepted address])}}))) + (unknown-realm-error {:realm-error realm-error + :erase-button erase-button}))))) (fx/defn open-login [{:keys [db]} address photo-path name] {:db (-> db @@ -212,8 +232,8 @@ (re-frame/reg-fx :accounts.login/login - (fn [[address password save-password?]] - (login! address (security/safe-unmask-data password) save-password?))) + (fn [[address password]] + (login! address (security/safe-unmask-data password)))) (re-frame/reg-fx :accounts.login/verify diff --git a/src/status_im/events.cljs b/src/status_im/events.cljs index 80c479917c..46c831baf9 100644 --- a/src/status_im/events.cljs +++ b/src/status_im/events.cljs @@ -110,7 +110,7 @@ (re-frame/inject-cofx :data-store/all-browsers) (re-frame/inject-cofx :data-store/all-dapp-permissions)] (fn [{:keys [db] :as cofx} [_ address]] - (let [{:node/keys [status]} db] + (let [{:node/keys [status on-ready]} db] (fx/merge cofx (if (= status :started) diff --git a/src/status_im/ui/screens/views.cljs b/src/status_im/ui/screens/views.cljs index 3d7407311a..10b2f01c92 100644 --- a/src/status_im/ui/screens/views.cljs +++ b/src/status_im/ui/screens/views.cljs @@ -144,7 +144,7 @@ :hardwallet-setup hardwallet-setup :hardwallet-success hardwallet-success))) (cond-> {:headerMode "none"} - (#{:intro :login} view-id) + (#{:intro :login :progress} view-id) (assoc :initialRouteName (name view-id))))} :chat-stack {:screen @@ -403,7 +403,7 @@ (when (and platform/android? (not js/goog.DEBUG) - (not (contains? #{:intro :login} @view-id))) + (not (contains? #{:intro :login :progress} @view-id))) (navigation/navigate-to @view-id))) ;; see https://reactnavigation.org/docs/en/state-persistence.html#development-mode :persistenceKey (when js/goog.DEBUG rand-label)}]))})))