forked from naoufal/react-native-touch-id
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TouchID.ios.js
62 lines (51 loc) · 1.45 KB
/
TouchID.ios.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
/**
* @providesModule TouchID
* @flow
*/
'use strict';
import { NativeModules } from 'react-native';
const NativeTouchID = NativeModules.TouchID;
const { iOSErrors } = require('./data/errors');
const { getError, TouchIDError, TouchIDUnifiedError } = require('./errors');
/**
* High-level docs for the TouchID iOS API can be written here.
*/
export default {
isSupported(config) {
return new Promise((resolve, reject) => {
NativeTouchID.isSupported(config, (error, biometryType) => {
if (error) {
return reject(createError(config, error.message));
}
resolve(biometryType);
});
});
},
authenticate(reason, config) {
const DEFAULT_CONFIG = {
fallbackLabel: null,
unifiedErrors: false,
passcodeFallback: false
};
const authReason = reason ? reason : ' ';
const authConfig = Object.assign({}, DEFAULT_CONFIG, config);
return new Promise((resolve, reject) => {
NativeTouchID.authenticate(authReason, authConfig, error => {
// Return error if rejected
if (error) {
return reject(createError(authConfig, error.message));
}
resolve(true);
});
});
}
};
function createError(config, error) {
const { unifiedErrors } = config || {};
if (unifiedErrors) {
return new TouchIDUnifiedError(getError(error));
}
const details = iOSErrors[error];
details.name = error;
return new TouchIDError(error, details);
}