Skip to content

Commit

Permalink
Merge pull request #6 from naoufal/feature/add-reason
Browse files Browse the repository at this point in the history
Feature/add reason
  • Loading branch information
naoufal committed Jul 19, 2015
2 parents 1a53a58 + 680b4b8 commit a66ee1b
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 71 deletions.
30 changes: 30 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"rules": {
"indent": [
2,
2
],
"quotes": [
2,
"single"
],
"linebreak-style": [
2,
"unix"
],
"semi": [
2,
"always"
]
},
"env": {
"es6": true,
"node": true
},
"ecmaFeatures": {
"jsx": true
},
"plugins": [
"react"
]
}
4 changes: 4 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"esnext": true,
"node": true
}
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- "0.12"
- "iojs"
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ __`react-native-touch-id`__ is a React Native library for authenticating users w
- [Example](https://github.com/naoufal/react-native-touch-id#example)
- [Methods](https://github.com/naoufal/react-native-touch-id#methods)
- [Errors](https://github.com/naoufal/react-native-touch-id#errors)
- [Todo](https://github.com/naoufal/react-native-touch-id#todo)
- [License](https://github.com/naoufal/react-native-touch-id#license)

## Install
Expand All @@ -35,7 +34,7 @@ var TouchID = require('react-native-touch-id');

Requesting Touch ID authentication is as simple as calling:
```js
TouchID.authenticate()
TouchID.authenticate('to demo this react-native component')
.then(success => {
// Success code
})
Expand All @@ -51,7 +50,7 @@ var TouchID = require('react-native-touch-id');

var YourComponent = React.createClass({
_pressHandler() {
TouchID.authenticate()
TouchID.authenticate('to demo this react-native component')
.then(success => {
AlertIOS.alert('Authenticated Successfully');
})
Expand All @@ -76,13 +75,16 @@ var YourComponent = React.createClass({
```

## Methods
### authenticate()
### authenticate(reason)
Attempts to authenticate with Touch ID.
Returns a `Promise` object.

__Arguments__
- `reason` - An _optional_ `String` that provides a clear reason for requesting authentication.

__Examples__
```js
TouchID.authenticate()
TouchID.authenticate('to demo this react-native component')
.then(success => {
// Success code
console.log('User authenticated with TouchID');
Expand Down Expand Up @@ -129,9 +131,6 @@ Below is a list of error codes that can be returned:

_More information on errors can be found in [Apple's Documentation](https://developer.apple.com/library/prerelease/ios/documentation/LocalAuthentication/Reference/LAContext_Class/index.html#//apple_ref/c/tdef/LAError)._

## Todo
- [ ] Add `authReason` argument

## License
Copyright (c) 2015, Naoufal Kadhom

Expand Down
2 changes: 1 addition & 1 deletion TouchID.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var warning = require('warning');

var TouchID = {
test: function() {
warning("Not yet implemented for Android.");
warning('Not yet implemented for Android.');
}
};

Expand Down
43 changes: 31 additions & 12 deletions TouchID.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@
*/
'use strict';

var NativeTouchID = require('NativeModules').TouchID;
var invariant = require('invariant');
var React = require('react-native');
var {
NativeModules
} = React;

var NativeTouchID = NativeModules.TouchID;
var ERRORS = require('./data/errors');

/*eslint-disable no-unused-vars*/
var invariant = require('invariant');
/*eslint-enable no-unused-vars*/


/**
* High-level docs for the TouchID iOS API can be written here.
*/

var TouchID = {
isSupported: function() {
return new Promise(function(resolve, reject) {
NativeTouchID.isSupported(function(error, supported) {
NativeTouchID.isSupported(function(error) {
if (error) {
return reject(createError(error.message));
}
Expand All @@ -25,9 +34,19 @@ var TouchID = {
});
},

authenticate() {
authenticate(reason) {
var authReason;

// Set auth reason
if (reason) {
authReason = reason;
// Set as empty string if no reason is passed
} else {
authReason = ' ';
}

return new Promise(function(resolve, reject) {
NativeTouchID.authenticate(function(error, success) {
NativeTouchID.authenticate(authReason, function(error) {
// Return error if rejected
if (error) {
return reject(createError(error.message));
Expand All @@ -39,13 +58,6 @@ var TouchID = {
}
};

function createError(error) {
var details = ERRORS[error];
details.name = error;

return new TouchIDError(error, details);
}

function TouchIDError(name, details) {
this.name = name || 'TouchIDError';
this.message = details.message || 'Touch ID Error';
Expand All @@ -55,4 +67,11 @@ function TouchIDError(name, details) {
TouchIDError.prototype = Object.create(Error.prototype);
TouchIDError.prototype.constructor = TouchIDError;

function createError(error) {
var details = ERRORS[error];
details.name = error;

return new TouchIDError(error, details);
}

module.exports = TouchID;
41 changes: 21 additions & 20 deletions TouchID.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ @implementation TouchID
{
LAContext *context = [[LAContext alloc] init];
NSError *error;

if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
callback(@[[NSNull null], @true]);
// Device does not support TouchID
Expand All @@ -20,65 +20,66 @@ @implementation TouchID
}
}

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

// Device has TouchID
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
// Attempt Authentification
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason: @" "
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";
errorReason = @"LAErrorUserFallback";
break;

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

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

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

case LAErrorTouchIDNotEnrolled:
errorReason = @"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)]);
Expand Down
5 changes: 2 additions & 3 deletions TouchID.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@
HEADER_SEARCH_PATHS = (
"$(inherited)",
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
"$(SRCROOT)/../../React/**",
"$(SRCROOT)/../../node_modules/react-native/React/**",
"$(SRCROOT)/../react-native/React/**",
);
LIBRARY_SEARCH_PATHS = "$(inherited)";
OTHER_LDFLAGS = "-ObjC";
Expand All @@ -215,7 +214,7 @@
HEADER_SEARCH_PATHS = (
"$(inherited)",
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
"$(SRCROOT)/../../React/**",
"$(SRCROOT)/../react-native/React/**",
);
LIBRARY_SEARCH_PATHS = "$(inherited)";
OTHER_LDFLAGS = "-ObjC";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>9F5B1B62-8B3F-400B-9F40-F12FFE3EF071</string>
<key>IDESourceControlProjectName</key>
<string>project</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>1CD020374FA2F1A50BAE8C5E41B060AD6A9501A1</key>
<string>github.com:naoufal/react-native-touch-id.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>TouchID.xcodeproj/project.xcworkspace</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>1CD020374FA2F1A50BAE8C5E41B060AD6A9501A1</key>
<string>../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>github.com:naoufal/react-native-touch-id.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>111</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>1CD020374FA2F1A50BAE8C5E41B060AD6A9501A1</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>1CD020374FA2F1A50BAE8C5E41B060AD6A9501A1</string>
<key>IDESourceControlWCCName</key>
<string>react-native-touch-id</string>
</dict>
</array>
</dict>
</plist>
Binary file not shown.
Loading

0 comments on commit a66ee1b

Please sign in to comment.