implemented login flow on startup

This commit is contained in:
Adrian Tiberius 2016-07-05 19:59:03 +03:00
parent 6841e8e430
commit 14c2b19402
8 changed files with 39 additions and 30 deletions

View File

@ -7,6 +7,7 @@
[status-im.persistence.simple-kv-store :as kv] [status-im.persistence.simple-kv-store :as kv]
[status-im.protocol.state.storage :as storage] [status-im.protocol.state.storage :as storage]
[status-im.utils.identicon :refer [identicon]] [status-im.utils.identicon :refer [identicon]]
[status-im.db :refer [default-view]]
[clojure.string :as str])) [clojure.string :as str]))
@ -34,8 +35,7 @@
(do (do
(save-password password) (save-password password)
(dispatch [:add-account account]) (dispatch [:add-account account])
(dispatch [:login-account address password]) (dispatch [:login-account address password])))))
(dispatch [:initialize-protocol account])))))
(register-handler :create-account (register-handler :create-account
(-> (fn [db [_ password]] (-> (fn [db [_ password]]
@ -45,7 +45,19 @@
(register-handler :login-account (register-handler :login-account
(-> (fn [db [_ address password]] (-> (fn [db [_ address password]]
(.login geth address password (fn [result] (.login geth address password (fn [result]
(log/debug "Logged in account: " address result) (let [account (get-in db [:accounts address])]
(dispatch [:set :current-account (get-in db [:accounts address])]))) (log/debug "Logged in account: " address result)
(dispatch [:set :login {}])
(dispatch [:set :current-account account])
(dispatch [:initialize-protocol account])
(dispatch [:navigate-to-clean default-view]))))
db))) db)))
(defn load-accounts! [db _]
(let [accounts (->> (accounts/get-accounts)
(map (fn [{:keys [address] :as account}]
[address account]))
(into {}))]
(assoc db :accounts accounts)))
(register-handler :load-accounts load-accounts!)

View File

@ -8,7 +8,7 @@
[status-im.components.react :refer [navigator app-registry device-event-emitter [status-im.components.react :refer [navigator app-registry device-event-emitter
orientation]] orientation]]
[status-im.components.main-tabs :refer [main-tabs]] [status-im.components.main-tabs :refer [main-tabs]]
[status-im.contacts.views.contact-list :refer [contact-list] ] [status-im.contacts.views.contact-list :refer [contact-list]]
[status-im.contacts.views.new-contact :refer [new-contact]] [status-im.contacts.views.new-contact :refer [new-contact]]
[status-im.qr-scanner.screen :refer [qr-scanner]] [status-im.qr-scanner.screen :refer [qr-scanner]]
[status-im.discovery.screen :refer [discovery]] [status-im.discovery.screen :refer [discovery]]
@ -23,7 +23,8 @@
[status-im.group-settings.screen :refer [group-settings]] [status-im.group-settings.screen :refer [group-settings]]
[status-im.profile.screen :refer [profile my-profile]] [status-im.profile.screen :refer [profile my-profile]]
[status-im.utils.utils :refer [toast]] [status-im.utils.utils :refer [toast]]
[status-im.utils.encryption])) [status-im.utils.encryption]
[status-im.utils.logging :as log]))
(defn init-back-button-handler! [] (defn init-back-button-handler! []
(let [new-listener (fn [] (let [new-listener (fn []
@ -42,7 +43,9 @@
(defn app-root [] (defn app-root []
(let [signed-up (subscribe [:get :signed-up]) (let [signed-up (subscribe [:get :signed-up])
view-id (subscribe [:get :view-id]) view-id (subscribe [:get :view-id])
account (subscribe [:get :current-account])
keyboard-height (subscribe [:get :keyboard-height])] keyboard-height (subscribe [:get :keyboard-height])]
(log/debug "Current account: " @account)
(r/create-class (r/create-class
{:component-will-mount {:component-will-mount
(fn [] (fn []
@ -64,7 +67,9 @@
#(dispatch [:set :keyboard-height 0])))) #(dispatch [:set :keyboard-height 0]))))
:render :render
(fn [] (fn []
(case (if @signed-up @view-id :chat) (let [startup-view (if @account @view-id (if (= @view-id :login) :login :users))]
(log/debug startup-view)
(case (if @signed-up startup-view :chat)
:discovery [main-tabs] :discovery [main-tabs]
:discovery-tag [discovery-tag] :discovery-tag [discovery-tag]
:add-participants [new-participants] :add-participants [new-participants]
@ -80,12 +85,13 @@
:profile [profile] :profile [profile]
:users [accounts] :users [accounts]
:login [login] :login [login]
:my-profile [my-profile]))}))) :my-profile [my-profile])))})))
(defn init [] (defn init []
(dispatch-sync [:initialize-db]) (dispatch-sync [:initialize-db])
(dispatch [:initialize-crypt]) (dispatch [:initialize-crypt])
(dispatch [:initialize-geth]) (dispatch [:initialize-geth])
(dispatch [:load-accounts])
(dispatch [:initialize-chats]) (dispatch [:initialize-chats])
;protocol must be initialized after user enters password and we create account ;protocol must be initialized after user enters password and we create account
;(dispatch [:initialize-protocol]) ;(dispatch [:initialize-protocol])

View File

@ -12,6 +12,7 @@
(def app-db {:identity-password "replace-me-with-user-entered-password" (def app-db {:identity-password "replace-me-with-user-entered-password"
:identity "me" :identity "me"
:accounts {} :accounts {}
:current-account false
:contacts [] :contacts []
:contacts-ids #{} :contacts-ids #{}
:selected-contacts #{} :selected-contacts #{}

View File

@ -79,12 +79,7 @@
(dispatch [:crypt-initialized])))))))) (dispatch [:crypt-initialized]))))))))
(defn node-started [db result] (defn node-started [db result]
(let [identity (:user-identity db) (log/debug "Started Node: " result))
password (:password db)]
(log/debug "Started Node: " result)
(when identity (do
(dispatch [:login-account (:address identity) password])
(dispatch [:initialize-protocol identity])))))
(register-handler :initialize-geth (register-handler :initialize-geth
(u/side-effect! (u/side-effect!

View File

@ -7,17 +7,4 @@
[{:keys [login] :as db} [_ _ login-info]] [{:keys [login] :as db} [_ _ login-info]]
(assoc db :login (merge login login-info))) (assoc db :login (merge login login-info)))
(register-handler :set-login-from-qr set-login-from-qr) (register-handler :set-login-from-qr set-login-from-qr)
(defn go-back
[_ _]
(dispatch [:navigate-back]))
(defn login
[{:keys [login] :as db} [_ _]]
(assoc db :current-account {:address (:address login)}))
(register-handler :login-account
(-> login
((after go-back))))

View File

@ -52,7 +52,7 @@
""]) ""])
(defview login [] (defview login []
[{:keys [address]} [:get :login]] [{:keys [address password]} [:get :login]]
[view st/screen-container [view st/screen-container
[linear-gradient {:colors ["rgba(182, 116, 241, 1)" "rgba(107, 147, 231, 1)" "rgba(43, 171, 238, 1)"] [linear-gradient {:colors ["rgba(182, 116, 241, 1)" "rgba(107, 147, 231, 1)" "rgba(43, 171, 238, 1)"]
:start [0, 0] :start [0, 0]
@ -78,6 +78,6 @@
[text {:style st/recover-text} (label :t/recover-from-passphrase)]]] [text {:style st/recover-text} (label :t/recover-from-passphrase)]]]
[view st/connect-button-container [view st/connect-button-container
[touchable-highlight [touchable-highlight
{:on-press #(dispatch [:login-account])} {:on-press #(dispatch [:login-account address password])}
[view st/connect-button [view st/connect-button
[text {:style st/connect-button-text} (label :t/connect)]]]]]]) [text {:style st/connect-button-text} (label :t/connect)]]]]]])

View File

@ -80,3 +80,11 @@
(push-view :profile))) (push-view :profile)))
(register-handler :show-profile show-profile) (register-handler :show-profile show-profile)
(defn navigate-to-clean
[db [_ view-id]]
(-> db
(assoc :navigation-stack (list))
(push-view view-id)))
(register-handler :navigate-to-clean navigate-to-clean)