Skip to content

Commit

Permalink
fix: addressed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
clauxx committed Jan 19, 2024
1 parent 2b878eb commit 330c607
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
9 changes: 9 additions & 0 deletions .clj-kondo/funcool/promesa/config.edn
Original file line number Diff line number Diff line change
@@ -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}}
47 changes: 23 additions & 24 deletions src/react_native/biometrics.cljs
Original file line number Diff line number Diff line change
@@ -1,69 +1,68 @@
(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
device, regardless of whether it's enabled or disabled.
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
Expand Down
8 changes: 3 additions & 5 deletions src/status_im/common/biometric/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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}))]
Expand All @@ -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]}
Expand Down

0 comments on commit 330c607

Please sign in to comment.