Skip to content

Commit

Permalink
Add FaceID detection
Browse files Browse the repository at this point in the history
  • Loading branch information
naoufal committed Nov 5, 2017
1 parent 3ca5ec9 commit 81185c0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
3 changes: 2 additions & 1 deletion TouchID.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import <React/RCTBridgeModule.h>
#import <LocalAuthentication/LocalAuthentication.h>

@interface TouchID : NSObject <RCTBridgeModule>

- (NSString *_Nonnull)getBiometryType:(LAContext *_Nonnull)context;
@end
4 changes: 2 additions & 2 deletions TouchID.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ const ERRORS = require('./data/errors');
export default {
isSupported() {
return new Promise((resolve, reject) => {
NativeTouchID.isSupported(error => {
NativeTouchID.isSupported((error, biometryType) => {
if (error) {
return reject(createError(error.message));
}

resolve(true);
resolve(biometryType);
});
});
},
Expand Down
41 changes: 25 additions & 16 deletions TouchID.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#import "TouchID.h"
#import <React/RCTUtils.h>
#import <LocalAuthentication/LocalAuthentication.h>

@implementation TouchID

Expand All @@ -10,9 +9,9 @@ @implementation TouchID
{
LAContext *context = [[LAContext alloc] init];
NSError *error;

if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
callback(@[[NSNull null], @true]);
callback(@[[NSNull null], [self getBiometryType:context]]);
// Device does not support TouchID
} else {
callback(@[RCTMakeError(@"RCTTouchIDNotSupported", nil, nil)]);
Expand All @@ -21,11 +20,11 @@ @implementation TouchID
}

RCT_EXPORT_METHOD(authenticate: (NSString *)reason
callback: (RCTResponseSenderBlock)callback)
callback: (RCTResponseSenderBlock)callback)
{
LAContext *context = [[LAContext alloc] init];
NSError *error;

// Device has TouchID
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
// Attempt Authentification
Expand All @@ -36,55 +35,65 @@ @implementation TouchID
// 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;

case LAErrorTouchIDNotAvailable:
errorReason = @"LAErrorTouchIDNotAvailable";
break;

case LAErrorTouchIDNotEnrolled:
errorReason = @"LAErrorTouchIDNotEnrolled";
break;

default:
errorReason = @"RCTTouchIDUnknownError";
break;
}

NSLog(@"Authentication failed: %@", errorReason);
callback(@[RCTMakeError(errorReason, nil, nil)]);
return;
}

// Authenticated Successfully
callback(@[[NSNull null], @"Authenticat with Touch ID."]);
}];

// Device does not support TouchID
} else {
callback(@[RCTMakeError(@"RCTTouchIDNotSupported", nil, nil)]);
return;
}
}

- (NSString *)getBiometryType:(LAContext *)context
{
if (@available(iOS 11, *)) {
return context.biometryType == LABiometryTypeFaceID ? @"FaceID" : @"TouchID";
}

return @"TouchID";
}

@end

0 comments on commit 81185c0

Please sign in to comment.