Skip to content

Commit

Permalink
Update Plugin.swift
Browse files Browse the repository at this point in the history
faceid
  • Loading branch information
baryon committed May 2, 2020
1 parent c41afc0 commit 92c091a
Showing 1 changed file with 48 additions and 40 deletions.
88 changes: 48 additions & 40 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class BiometricAuth: CAPPlugin {
let localAuthenticationContext = LAContext()
localAuthenticationContext.localizedFallbackTitle = "Use Passcode"
if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
call.resolve(["has": true])
call.resolve(["has": true, "faceID": localAuthenticationContext.biometryType == .faceID, "touchID": localAuthenticationContext.biometryType == .touchID])
} else {
guard let error = authError else {
return
Expand All @@ -39,64 +39,72 @@ public class BiometricAuth: CAPPlugin {
switch error.code {
case LAError.touchIDLockout.rawValue:
errorCode = 2

case LAError.touchIDNotAvailable.rawValue:
errorCode = 1

case LAError.touchIDNotEnrolled.rawValue:
errorCode = 3

default:
errorCode = 999
}
}

call.resolve(["has": false, "status": errorCode])
}
}

@objc func verify(_ call: CAPPluginCall) {
let localAuthenticationContext = LAContext()
let reasonString = call.getString("reason") ?? "To access the secure data"
localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reasonString) { success, evaluateError in

localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString) { success, evaluateError in
if success {
call.resolve(["verified": true])
} else {
var errorCode = 0
guard let error = evaluateError else {
return
}
switch error._code {

case LAError.authenticationFailed.rawValue:
errorCode = 10 //"The user failed to provide valid credentials"

case LAError.appCancel.rawValue:
errorCode = 11 // "Authentication was cancelled by application"

case LAError.invalidContext.rawValue:
errorCode = 12 // "The context is invalid"

case LAError.notInteractive.rawValue:
errorCode = 13 // "Not interactive"

case LAError.passcodeNotSet.rawValue:
errorCode = 14 // "Passcode is not set on the device"

case LAError.systemCancel.rawValue:
errorCode = 15 // "Authentication was cancelled by the system"

case LAError.userCancel.rawValue:
errorCode = 16 // "The user did cancel"

case LAError.userFallback.rawValue:
errorCode = 17 // "The user chose to use the fallback"

default:
errorCode = self.evaluatePolicyFailErrorMessageForLA(errorCode: error._code)
}

call.reject("Auth failed", nil, ["verified": false, "status": errorCode] as? Error)
localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reasonString) { success, evaluateError in
if success {
call.resolve(["verified": true])
} else {
var errorCode = 0
guard let error = evaluateError else {
return
}
switch error._code {

case LAError.authenticationFailed.rawValue:
errorCode = 10 //"The user failed to provide valid credentials"

case LAError.appCancel.rawValue:
errorCode = 11 // "Authentication was cancelled by application"

case LAError.invalidContext.rawValue:
errorCode = 12 // "The context is invalid"

case LAError.notInteractive.rawValue:
errorCode = 13 // "Not interactive"

case LAError.passcodeNotSet.rawValue:
errorCode = 14 // "Passcode is not set on the device"

case LAError.systemCancel.rawValue:
errorCode = 15 // "Authentication was cancelled by the system"

case LAError.userCancel.rawValue:
errorCode = 16 // "The user did cancel"

case LAError.userFallback.rawValue:
errorCode = 17 // "The user chose to use the fallback"

default:
errorCode = self.evaluatePolicyFailErrorMessageForLA(errorCode: error._code)
}

call.reject("Auth failed", nil, ["verified": false, "status": errorCode] as? Error)
}
}
}
}
}
Expand Down

0 comments on commit 92c091a

Please sign in to comment.