|
1 | 1 | import { Observable, EventData, Page } from '@nativescript/core';
|
2 |
| -import {} from '@nativescript/fingerprint-auth'; |
| 2 | +import { FingerprintAuth, BiometricIDAvailableResult } from '@nativescript/fingerprint-auth'; |
3 | 3 |
|
4 | 4 | export function navigatingTo(args: EventData) {
|
5 | 5 | const page = <Page>args.object;
|
6 | 6 | page.bindingContext = new DemoModel();
|
7 | 7 | }
|
8 | 8 |
|
9 | 9 | export class DemoModel extends Observable {
|
10 |
| - testIt() { |
11 |
| - // Test something here |
| 10 | + private fingerprintAuth: FingerprintAuth; |
| 11 | + public status: string = 'Tap a button below..'; |
| 12 | + private static CONFIGURED_PASSWORD = 'MyPassword'; |
| 13 | + |
| 14 | + constructor() { |
| 15 | + super(); |
| 16 | + this.fingerprintAuth = new FingerprintAuth(); |
| 17 | + } |
| 18 | + |
| 19 | + public doCheckAvailable(): void { |
| 20 | + this.fingerprintAuth |
| 21 | + .available() |
| 22 | + .then((result: BiometricIDAvailableResult) => { |
| 23 | + console.log('doCheckAvailable result: ' + JSON.stringify(result)); |
| 24 | + this.set('status', 'Biometric ID available? - ' + (result.any ? (result.face ? 'Face' : 'Touch') : 'NO')); |
| 25 | + }) |
| 26 | + .catch((err) => { |
| 27 | + console.log('doCheckAvailable error: ' + err); |
| 28 | + this.set('status', 'Error: ' + err); |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + public doCheckFingerprintsChanged(): void { |
| 33 | + this.fingerprintAuth.didFingerprintDatabaseChange().then((changed: boolean) => { |
| 34 | + this.set('status', 'Biometric ID changed? - ' + (changed ? 'YES' : 'NO')); |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + public doVerifyFingerprint(): void { |
| 39 | + this.fingerprintAuth |
| 40 | + .verifyFingerprint({ |
| 41 | + title: 'Enter your password', |
| 42 | + message: 'Scan yer finger', // optional |
| 43 | + authenticationValidityDuration: 10, // Android |
| 44 | + }) |
| 45 | + .then(() => this.set('status', 'Biometric ID / passcode OK')) |
| 46 | + .catch((err) => { |
| 47 | + alert({ |
| 48 | + title: 'Biometric ID NOT OK / canceled', |
| 49 | + message: JSON.stringify(err), |
| 50 | + okButtonText: 'Mmkay', |
| 51 | + }); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + public doVerifyFingerprintWithCustomUI(): void { |
| 56 | + this.fingerprintAuth |
| 57 | + .verifyFingerprint({ |
| 58 | + message: 'Scan yer finger', // optional |
| 59 | + useCustomAndroidUI: true, // Android |
| 60 | + }) |
| 61 | + .then((enteredPassword?: string) => { |
| 62 | + if (enteredPassword === undefined) { |
| 63 | + this.set('status', 'Biometric ID OK'); |
| 64 | + } else { |
| 65 | + // compare enteredPassword to the one the user previously configured for your app (which is not the users system password!) |
| 66 | + if (enteredPassword === DemoModel.CONFIGURED_PASSWORD) { |
| 67 | + this.set('status', 'Biometric ID OK, using password'); |
| 68 | + } else { |
| 69 | + this.set('status', `Wrong password. Try '${DemoModel.CONFIGURED_PASSWORD}' 😉`); |
| 70 | + } |
| 71 | + } |
| 72 | + }) |
| 73 | + .catch((err) => this.set('status', `Biometric ID NOT OK: " + ${JSON.stringify(err)}`)); |
| 74 | + } |
| 75 | + |
| 76 | + public doVerifyFingerprintWithCustomFallback(): void { |
| 77 | + this.fingerprintAuth |
| 78 | + .verifyFingerprintWithCustomFallback({ |
| 79 | + message: 'Scan yer finger', // optional |
| 80 | + fallbackMessage: 'Enter PIN', // optional |
| 81 | + authenticationValidityDuration: 10, // Android |
| 82 | + }) |
| 83 | + .then(() => this.set('status', 'Biometric ID OK')) |
| 84 | + .catch((error) => { |
| 85 | + this.set('status', 'Biometric ID NOT OK: ' + JSON.stringify(error)); |
| 86 | + alert({ |
| 87 | + title: 'Biometric ID NOT OK', |
| 88 | + message: error.code === -3 ? 'Show custom fallback' : error.message, |
| 89 | + okButtonText: 'Mmkay', |
| 90 | + }); |
| 91 | + }); |
12 | 92 | }
|
13 | 93 | }
|
0 commit comments