forked from naoufal/react-native-passcode-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPasscodeAuth.m
102 lines (79 loc) · 3.07 KB
/
PasscodeAuth.m
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
93
94
95
96
97
98
99
100
101
102
#import "PasscodeAuth.h"
#import "RCTUtils.h"
#import <LocalAuthentication/LocalAuthentication.h>
@implementation PasscodeAuth
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(isSupported: (RCTResponseSenderBlock)callback)
{
if (![self supportsPasscodeAuth]) {
callback(@[RCTMakeError(@"PasscodeAuthNotSupported", nil, nil)]);
return;
}
LAContext *context = [[LAContext alloc] init];
// Check if PasscodeAuth Authentication is available
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:nil]) {
callback(@[[NSNull null], @true]);
// PasscodeAuth is not set
} else {
callback(@[RCTMakeError(@"PasscodeAuthNotSet", nil, nil)]);
return;
}
}
RCT_EXPORT_METHOD(authenticate: (NSString *)reason
callback: (RCTResponseSenderBlock)callback)
{
if (![self supportsPasscodeAuth]) {
callback(@[RCTMakeError(@"PasscodeAuthNotSupported", nil, nil)]);
return;
}
LAContext *context = [[LAContext alloc] init];
NSError *error;
// Device has PasscodeAuth
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error]) {
// Attempt Authentication
[context evaluatePolicy:LAPolicyDeviceOwnerAuthentication
localizedReason:reason
reply:^(BOOL success, NSError *error)
{
// Failed Authentication
if (error) {
NSString *errorReason;
switch (error.code) {
case LAErrorAuthenticationFailed:
errorReason = @"LAErrorAuthenticationFailed";
break;
case LAErrorUserCancel:
errorReason = @"LAErrorUserCancel";
break;
case LAErrorUserFallback:
errorReason = @"LAErrorUserFallback";
break;
case LAErrorSystemCancel:
errorReason = @"LAErrorSystemCancel";
break;
case LAErrorPasscodeNotSet:
errorReason = @"LAErrorPasscodeNotSet";
break;
default:
errorReason = @"PasscodeAuthUnknownError";
break;
}
NSLog(@"Authentication failed: %@", errorReason);
callback(@[RCTMakeError(errorReason, nil, nil)]);
return;
}
// Authenticated Successfully
callback(@[[NSNull null], @"Authenticated with PasscodeAuth."]);
}];
// Device does not support PasscodeAuth
} else {
callback(@[RCTMakeError(@"PasscodeAuthNotSet", nil, nil)]);
return;
}
}
- (BOOL)supportsPasscodeAuth {
// PasscodeAuth is only available in iOS 9. In iOS8, `LAPolicyDeviceOwnerAuthentication` is present but not implemented.
float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
return osVersion >= 9.0;
}
@end