import { BiometricIDAvailableResult , FingerprintAuth } from "nativescript-fingerprint-auth" ; import { Observable } from "tns-core-modules/data/observable" ; import { alert } from "tns-core-modules/ui/dialogs" ; export class HelloWorldModel extends Observable { private fingerprintAuth : FingerprintAuth ; public status : string = "Tap a button below.." ; private static CONFIGURED_PASSWORD = "MyPassword" ; constructor ( ) { super ( ) ; this . fingerprintAuth = new FingerprintAuth ( ) ; } public doCheckAvailable ( ) { this . fingerprintAuth . available ( ) . then ( ( result ) => { console . log ( "doCheckAvailable result: " + JSON . stringify ( result ) ) ; this . set ( "status" , "Biometric ID available? - " + ( result . any ? ( result . face ? "Face" : "Touch" ) : "NO" ) ) ; } ) . catch ( err => { console . log ( "doCheckAvailable error: " + err ) ; this . set ( "status" , "Error: " + err ) ; } ) ; } public doCheckFingerprintsChanged ( ) { this . fingerprintAuth . didFingerprintDatabaseChange ( ) . then ( ( changed ) => { this . set ( "status" , "Biometric ID changed? - " + ( changed ? "YES" : "NO" ) ) ; } ) ; } public doVerifyFingerprint ( ) { this . fingerprintAuth . verifyFingerprint ( { title : "Enter your password" , message : "Scan yer finger" , authenticationValidityDuration : 10 } ) . then ( ( ) => this . set ( "status" , "Biometric ID / passcode OK" ) ) . catch ( err => { alert ( { title : "Biometric ID NOT OK / canceled" , message : JSON . stringify ( err ) , okButtonText : "Mmkay" } ) ; } ) ; } public doVerifyFingerprintWithCustomUI ( ) { this . fingerprintAuth . verifyFingerprint ( { message : "Scan yer finger" , useCustomAndroidUI : true } ) . then ( ( enteredPassword ? ) => { if ( enteredPassword === undefined ) { this . set ( "status" , "Biometric ID OK" ) ; } else { if ( enteredPassword === HelloWorldModel . CONFIGURED_PASSWORD ) { this . set ( "status" , "Biometric ID OK, using password" ) ; } else { this . set ( "status" , `Wrong password. Try '${ HelloWorldModel . CONFIGURED_PASSWORD }' 😉` ) ; } } } ) . catch ( err => this . set ( "status" , `Biometric ID NOT OK: " + ${ JSON . stringify ( err ) }` ) ) ; } public doVerifyFingerprintWithCustomFallback ( ) { this . fingerprintAuth . verifyFingerprintWithCustomFallback ( { message : "Scan yer finger" , fallbackMessage : "Enter PIN" , authenticationValidityDuration : 10 } ) . then ( ( ) => this . set ( "status" , "Biometric ID OK" ) ) . catch ( error => { this . set ( "status" , "Biometric ID NOT OK: " + JSON . stringify ( error ) ) ; alert ( { title : "Biometric ID NOT OK" , message : error . code === - 3 ? "Show custom fallback" : error . message , okButtonText : "Mmkay" } ) ; } ) ; } }