forked from naoufal/react-native-touch-id
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTouchID.android.js
92 lines (76 loc) · 3.07 KB
/
TouchID.android.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { NativeModules, processColor } from 'react-native';
import { androidApiErrorMap, androidModuleErrorMap } from './data/errors';
import { getError, TouchIDError, TouchIDUnifiedError } from './errors';
const NativeTouchID = NativeModules.FingerprintAuth;
const { codes, errors } = require('./data/errors');
export default {
isSupported(config) {
return new Promise((resolve, reject) => {
NativeTouchID.isSensorAvailable()
.then((resultObject) => {
const { available, biometryType, error } = resultObject;
if (available && biometryType === "Biometrics") { // this is the UNIVERSAL type in ANDROID (system selected biometric)
return resolve(biometryType);
} else {
return reject(createError(config, error, codes.androidModuleCodes.NOT_SUPPORTED));
}
})
.catch((error) => {
//console.log('Biometric isSupported', error);
return reject(createError(config, error, codes.androidModuleCodes.NOT_SUPPORTED));
});
});
},
authenticate(reason, config) {
var DEFAULT_CONFIG = {
title: 'Authentication Required',
subtitle: 'Touch/Face Id',
imageColor: '#1306ff',
imageErrorColor: '#ff0000',
sensorDescription: 'Touch sensor',
sensorErrorDescription: 'Failed',
cancelText: 'Cancel',
description: 'Sample App requires authentication',
unifiedErrors: false,
usecrypto: false,
};
var authReason = reason ? reason : ' ';
var authConfig = Object.assign({}, DEFAULT_CONFIG, config);
var imageColor = processColor(authConfig.imageColor);
var imageErrorColor = processColor(authConfig.imageErrorColor);
authConfig.imageColor = imageColor;
authConfig.imageErrorColor = imageErrorColor;
//console.log(authReason,authConfig);
return new Promise((resolve, reject) => {
NativeTouchID.simplePrompt({
title: authConfig.title,
subtitle: authConfig.subtitle,
cancel: authConfig.cancelText,
description: authConfig.description,
usecrypto : authConfig.usecrypto
})
.then((resultObject) => {
const { success,error } = resultObject
if (success) {
//console.log('Biometric', 'Success')
return resolve(true);
} else {
//console.log('Biometric', 'Cancelled',error);
return reject(createError(authConfig, "Cancelled", codes.androidModuleCodes.AUTHENTICATION_CANCELED));
}
})
.catch((sperror) => {
//console.log('Biometric XX Failed:', sperror);
return reject(createError(authConfig, "Unknown error", codes.androidModuleCodes.AUTHENTICATION_FAILED));
});
});
}
};
function createError(config, error, code) {
const { unifiedErrors } = config || {};
const errorCode = androidApiErrorMap[code] || androidModuleErrorMap[code];
if (unifiedErrors) {
return new TouchIDUnifiedError(getError(errorCode));
}
return new TouchIDError('Touch ID Error', error, errorCode);
}