forked from naoufal/react-native-touch-id
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TouchID.android.js
62 lines (55 loc) · 1.79 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
import { NativeModules, processColor } from 'react-native';
import { androidApiErrorMap, androidModuleErrorMap } from './data/errors';
import { getError, TouchIDError, TouchIDUnifiedError } from './errors';
const NativeTouchID = NativeModules.FingerprintAuth;
export default {
isSupported(config) {
return new Promise((resolve, reject) => {
NativeTouchID.isSupported(
(error, code) => {
return reject(createError(config, error, code));
},
(biometryType) => {
return resolve(biometryType);
}
);
});
},
authenticate(reason, config) {
var DEFAULT_CONFIG = {
title: 'Authentication Required',
imageColor: '#1306ff',
imageErrorColor: '#ff0000',
sensorDescription: 'Touch sensor',
sensorErrorDescription: 'Failed',
cancelText: 'Cancel',
unifiedErrors: 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;
return new Promise((resolve, reject) => {
NativeTouchID.authenticate(
authReason,
authConfig,
(error, code) => {
return reject(createError(authConfig, error, code));
},
success => {
return resolve(true);
}
);
});
}
};
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);
}