From 330c607d2186b94066b4492482c4d36024838c10 Mon Sep 17 00:00:00 2001 From: Lungu Cristian Date: Fri, 19 Jan 2024 10:54:56 +0200 Subject: [PATCH] fix: addressed review comments --- .clj-kondo/funcool/promesa/config.edn | 9 +++++ src/react_native/biometrics.cljs | 47 +++++++++++----------- src/status_im/common/biometric/events.cljs | 8 ++-- 3 files changed, 35 insertions(+), 29 deletions(-) create mode 100644 .clj-kondo/funcool/promesa/config.edn diff --git a/.clj-kondo/funcool/promesa/config.edn b/.clj-kondo/funcool/promesa/config.edn new file mode 100644 index 000000000000..1844f77c5a63 --- /dev/null +++ b/.clj-kondo/funcool/promesa/config.edn @@ -0,0 +1,9 @@ +{:lint-as {promesa.core/-> clojure.core/-> + promesa.core/->> clojure.core/->> + promesa.core/as-> clojure.core/as-> + promesa.core/let clojure.core/let + promesa.core/plet clojure.core/let + promesa.core/loop clojure.core/loop + promesa.core/recur clojure.core/recur + promesa.core/with-redefs clojure.core/with-redefs + promesa.core/doseq clojure.core/doseq}} diff --git a/src/react_native/biometrics.cljs b/src/react_native/biometrics.cljs index 319262021f79..db12a97462ee 100644 --- a/src/react_native/biometrics.cljs +++ b/src/react_native/biometrics.cljs @@ -1,13 +1,14 @@ (ns react-native.biometrics (:require - ["react-native-biometrics" :default biometrics] + ["react-native-biometrics" :default Biometrics] [clojure.string :as string] [oops.core :as oops] [react-native.platform :as platform] [schema.core :as schema])) -(def ^:const biometric-error-not-available "biometric-not-available") -(def ^:const biometric-error-not-enrolled "biometric-not-enrolled") +(def ^:private ^:const android-not-available-error-message "Biometric hardware unavailable") +(def ^:private ^:const android-not-enrolled-error-message "No fingerprints enrolled.") +(def ^:private ^:const ios-not-enrolled-error-message "No identities are enrolled") (defn get-supported-type "Returns a JS promise that resolves with the biometrics types supported by the @@ -15,55 +16,53 @@ Resolved values: `:Biometrics` `:FaceID` `:TouchID`" [] - (-> (biometrics.) + (-> (Biometrics.) (.isSensorAvailable) (.then (fn [result] - (let [type (-> result - (oops/oget "biometryType") - keyword)] - type))) + (-> result + (oops/oget :biometryType) + keyword))) (.catch (constantly nil)))) (defn get-available "Returns a JS promise that resolves to a boolean, which signifies whether biometrics is enabled/disabled on the device." [] - (-> (biometrics.) + (-> (Biometrics.) (.isSensorAvailable) (.then (fn [result] - (oops/oget result "available"))))) + (oops/oget result :available))))) ;; NOTE: the react-native-biometrics package error codes/messages differ across platforms. ;; On android we get error messages while on iOS it's a stringified Obj-C error object. (defn- convert-auth-error-message - [code] - (letfn [(includes? [substring string] - (string/includes? string substring))] - (if platform/android? - (condp = code - "No fingerprints enrolled." biometric-error-not-enrolled - "Biometric hardware unavailable" biometric-error-not-available - code) + [message] + (ex-info "Failed to authenticate with biometrics" + {:orig-error-message message + :code (if platform/android? + (condp = message + android-not-enrolled-error-message ::not-enrolled + android-not-available-error-message ::not-available + ::unknown) - (condp includes? code - "No identities are enrolled" biometric-error-not-enrolled - code)))) + (condp #(string/includes? %2 %1) message + ios-not-enrolled-error-message ::not-enrolled + ::unknown))})) (defn authenticate "Returns a JS promise that resolves with a boolean auth success state: `true` for success and `false` when canceled by user." [{:keys [prompt-message fallback-prompt-message cancel-button-text]}] - (-> (biometrics.) + (-> (Biometrics.) (.simplePrompt #js {"promptMessage" prompt-message "fallbackPromptMessage" fallback-prompt-message "cancelButtonText" cancel-button-text}) (.then (fn [result] - (-> result js->clj (get "success")))) + (oops/oget result :success))) (.catch (fn [error] (-> (.-message error) convert-auth-error-message - js/Error throw))))) (schema/=> authenticate diff --git a/src/status_im/common/biometric/events.cljs b/src/status_im/common/biometric/events.cljs index 3c7dcbc2b6e7..705ee59897c9 100644 --- a/src/status_im/common/biometric/events.cljs +++ b/src/status_im/common/biometric/events.cljs @@ -45,8 +45,8 @@ (rf/defn show-message {:events [:biometric/show-message]} [_ code] - (let [content (if (#{biometrics/biometric-error-not-available - biometrics/biometric-error-not-enrolled} + (let [content (if (#{::biometrics/not-enrolled + ::biometrics/not-available} code) (i18n/label :t/grant-face-id-permissions) (i18n/label :t/biometric-auth-error {:code code}))] @@ -68,9 +68,7 @@ (on-success)))) (.catch (fn [err] (when on-fail - (-> err - (.-message) - (on-fail)))))))) + (-> err ex-data :code on-fail))))))) (rf/defn authenticate {:events [:biometric/authenticate]}