Listen and send confirmation code

This commit is contained in:
virvar 2016-05-18 18:40:01 +03:00
parent 1f5dad71e8
commit 476d36f555
3 changed files with 29 additions and 9 deletions

View File

@ -229,8 +229,13 @@
(let [formatted (format-phone-number phone-number)] (let [formatted (format-phone-number phone-number)]
(-> db (-> db
(assoc :user-phone-number formatted) (assoc :user-phone-number formatted)
sign-up-service/start-listening-confirmation-code-sms
(server/sign-up formatted sign-up-service/on-sign-up-response))))) (server/sign-up formatted sign-up-service/on-sign-up-response)))))
(register-handler :stop-listening-confirmation-code-sms
(fn [db [_]]
(sign-up-service/stop-listening-confirmation-code-sms db)))
(register-handler :sign-up-confirm (register-handler :sign-up-confirm
(fn [db [_ confirmation-code]] (fn [db [_ confirmation-code]]
(server/sign-up-confirm confirmation-code sign-up-service/on-send-code-response) (server/sign-up-confirm confirmation-code sign-up-service/on-send-code-response)

View File

@ -51,6 +51,7 @@
:from "console" :from "console"
:to "me"}]) :to "me"}])
(when (:confirmed body) (when (:confirmed body)
(dispatch [:stop-listening-confirmation-code-sms])
(sync-contacts) (sync-contacts)
;; TODO should be called after sync-contacts? ;; TODO should be called after sync-contacts?
(dispatch [:set-signed-up true]))) (dispatch [:set-signed-up true])))
@ -75,9 +76,18 @@
:from "console" :from "console"
:to "me"}]))) :to "me"}])))
(defn start-listen-confirmation-code-sms [] (defn handle-sms [{body :body}]
;; TODO UNDONE listen sms (when-let [matches (re-matches #"(\d{4})" body)]
) (dispatch [:sign-up-confirm (second matches)])))
(defn start-listening-confirmation-code-sms [db]
(when (not (:confirmation-code-sms-listener db))
(assoc db :confirmation-code-sms-listener (add-sms-listener handle-sms))))
(defn stop-listening-confirmation-code-sms [db]
(when-let [listener (:confirmation-code-sms-listener db)]
(remove-sms-listener listener)
(dissoc db :confirmation-code-sms-listener)))
;; -- Saving password ---------------------------------------- ;; -- Saving password ----------------------------------------
(defn save-password [password] (defn save-password [password]

View File

@ -1,14 +1,19 @@
(ns syng-im.utils.sms-listener) (ns syng-im.utils.sms-listener
(:require [syng-im.components.react :refer [android?]]))
(def sms-listener (js/require "react-native-android-sms-listener")) (def sms-listener (.-default (js/require "react-native-android-sms-listener")))
;; Only android is supported! ;; Only android is supported!
(defn add-sms-listener (defn add-sms-listener
"Message format: {originatingAddress: string, body: "Message format: {:originatingAddress string, :body string}. Returns
string}. Returns cancelable subscription." cancelable subscription."
[listen-fn] [listen-fn]
(.addListener sms-listener listen-fn)) (when android?
(.addListener sms-listener
(fn [message]
(listen-fn (js->clj message :keywordize-keys true))))))
(defn remove-sms-listener [subscription] (defn remove-sms-listener [subscription]
(.remove subscription)) (when android?
(.remove subscription)))